0
後,我寫一個的Android服務從Instagram的每十秒鐘, 拉網絡數據,然後發送通知如果有什麼新的錯誤發生。的Android服務的工作,只有當USB插上,USB斷開
它工作得很好,當USB調試打開,(可以看到關於Logcat的東西) 但我斷開USB後會發生錯誤!
我不知道調試或檢查什麼是對那裏發生的日誌, 因爲錯誤時logcat的是不工作...
這裏有一些關於我的服務代碼(對不起,複雜性只發生。 。)
@Override
public void onCreate() {
super.onCreate();
// some initializations, fetch the access token, setup the API library ..
oauthSession = new InstagramOAuthSession(getApplicationContext());
String accessToken = oauthSession.getAccessToken();
instagramFacade = new InstagramFacadeImp(accessToken);
session = new InstagramDataSession(this);
try {
followers = session.readFollowers();
Log.d("myLog", "Read Result: " + followers);
} catch (IOException e) {
Log.d("myLog","Io Error: " + e.getMessage());
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
Log.d("myLog", "Service start");
handler.post(checkUnfollowedTask); //task is a runnable
return START_REDELIVER_INTENT;
}
private Runnable checkUnfollowedTask = new Runnable() {
@Override
public void run() {
AsyncTaskHelper.runAsyncTask(new AsyncTask<Void, Void, List<User>>() {
@Override
protected List<User> doInBackground(Void... voids) {
try {
//to pull some network data from instagram
FollowInfoViewModel model = instagramFacade.getFollowInfoViewModel();
//save data into the internal storage
session.saveFollowers(model.getFollowerUsers());
return model.getFollowerUsers();
} catch (Exception e) {
Log.d("myLog","Error: " + e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<User> users) {
super.onPostExecute(users);
//some business logics
Log.d("myLog", "New follower: " + users);
followers.removeAll(users);
Log.d("myLog", "New unfollower: " + followers);
createNotification(followers);
followers = users;
}
});
handler.postDelayed(this, 10000); // invoke every ten seconds
}
};
然後是所有有關創建通知的邏輯。
private void createNotification(List<User> unfollowers) {
if (unfollowers.size() > 0)
Log.d("myLog", "Unfollower detected, creatre a notification.");
NotificationManager notificationManager = (NotificationManager)
getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
List<Notification> notifications = buildNotifications(unfollowers);
for (int i = 0; i < notifications.size(); i++)
notificationManager.notify(i, notifications.get(i));
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private List<Notification> buildNotifications(List<User> unfollowers) {
List<Notification> notifications = new ArrayList<>();
Context context = getApplicationContext();
Notification.Builder builder = new Notification.Builder(context);
for (int i = 0; i < unfollowers.size(); i++)
{
User unfollower = unfollowers.get(i);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = builder.setSmallIcon(R.drawable.doge)
.setContentTitle(getString(R.string.receive_unfollow_notification))
.setContentText(unfollower.getFull_name() + "(" + unfollower.getUsername() + ")"+ getString(R.string.unfollowed_you))
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.build();
notifications.add(notification);
}
return notifications;
}
請幫我找出潛在的錯誤或提供我一些辦法調試,謝謝。