通过alarmManger取消通知时出现问题

时间:2016-01-28 21:07:16

标签: java android

我有一个由Notification触发的AlarmManager。这是我的代码:

    notification = new NotificationCompat.Builder(getActivity());

    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");

    notificationIntent.addCategory("android.intent.category.DEFAULT");

    PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 5000, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, 10000, broadcast);

    alarmManager.cancel(broadcast);
    // here am trying to cancel the notification with PendingIntent but its not working 

编辑:

通过工作我的意思是它不取消通知应该

1 个答案:

答案 0 :(得分:1)

使用

PendingIntent.getBroadcast(getContext(), 5000, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT).cancel();

而不是alarmManager.cancel(broadcast)

感谢这些帖子Cancelling a PendingIntent