我正在使用alarmmanager和服务开发应用程序,在某些情况下我无法取消通知。我正在使用片段。
我的应用每隔“x”分钟发送两个通知。我有一个启动服务的按钮和另一个停止它的按钮。当我启动服务时,会触发通知,当我按下停止按钮时,它可以正常工作。但是当我启动服务并退出应用程序时,通知会正常触发,但是当我点击通知时,应用程序会打开,然后我按停止按钮但通知永远不会停止,通知会继续被触发。
我调用alarmanager.cancel(pendingintent)的函数位于同一个片段中。(MainFragment.class是第一个片段)。
MainFragment.class
intent = new Intent(getContext(), TimeAlarm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.setAction(Long.toString(System.currentTimeMillis()));
pendingIntent = PendingIntent.getBroadcast(mContext, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 15000,
15000,
pendingIntent);
intent2 = new Intent(getContext(), TimeAlarm2.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent2.setAction(Long.toString(System.currentTimeMillis()));
pendingIntent2 = PendingIntent.getBroadcast(getContext(), 1,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 15000,
15000,
pendingIntent2);
TimeAlarm.class,它扩展了BroadcastReceiver
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("...")
.setContentText("...");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
TimeAlarm2.class,它扩展了BroadcastReceiver
NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon2)
.setContentTitle("..")
.setContentText("..");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder2.setContentIntent(resultPendingIntent);
mBuilder2.setAutoCancel(true);
mBuilder2.build().flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder2.build());
谢谢和最诚挚的问候!
答案 0 :(得分:1)
从两个意图中删除这些行并再次尝试。
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent2.setAction(Long.toString(System.currentTimeMillis()));
这对我有用
答案 1 :(得分:0)
试试这个,如果错误告诉我发生了什么。
为此,您需要在停止按钮上销毁服务,然后再次单击开始按钮启动服务。
答案 2 :(得分:0)
好。我建议您使用与cancel()
方法相同的PendingIntent,在 AlarmManager 上调用setRepeating()
。
您可以在这里取消两个等效的pendingIntent。
am.cancel(pendingIntent);
和
am.cancel(pendingIntent2);
希望它会对你有所帮助。