我正在编写一个 Android服务,每隔十秒就会从Instagram 中提取网络数据, 如果有任何新内容,请发送通知。
当 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;
}
请帮我找出潜在的错误或者给我一些调试方法,谢谢。
答案 0 :(得分:0)
您可以显示Toast或Alert对话框以显示错误..或实现自定义异常处理程序,请参阅以下答案:Android: Catch Error "Unfortunately 'app' has stopped working"