如何检测通知是否已被解除?

New*_*Dev 24 android repeat alarm alarmmanager notificationmanager

Android中是否有任何方法可以检测用户何时向左侧滑动通知并将其删除?我正在使用闹钟管理器来设置重复警报,当用户取消通知时,我需要停止重复警报.这是我的代码:

设置重复提醒:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);
Run Code Online (Sandbox Code Playgroud)

我的通知代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the notification ID.
    int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");

    //Get the name of the reminder.
    String reminderName = getIntent().getExtras().getString("Reminder_Name");

    //PendingIntent stores the Activity that should be launched when the user taps the notification.
    Intent i = new Intent(this, ViewLocalRemindersDetail.class);
    i.putExtra("NotifID", notifID);
    i.putExtra("notification_tap", true);

    //Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered.
    PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, i, Intent.FLAG_ACTIVITY_NEW_TASK);

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis());

    CharSequence from = "Here's your reminder:";

    CharSequence message = reminderName;        
    notif.setLatestEventInfo(this, from, message, displayIntent);

    //Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms.
    notif.defaults |= Notification.DEFAULT_SOUND;
    notif.vibrate = new long[] { 100, 250, 100, 500 };
    nm.notify(notifID, notif);

    //Destroy the activity/notification.
    finish();

}
Run Code Online (Sandbox Code Playgroud)

我知道我需要打电话alarmManager.cancel(displayIntent)才能取消重复闹铃.但是,我不明白在哪里放这个代码.我只需要在用户点击通知或解除通知时取消重复提醒.谢谢你的帮助!

And*_*ani 19

我相信这Notification.deleteIntent就是你要找的东西.医生说:

用户明确驳回通知时执行的意图,使用"全部清除"按钮或单独滑动它.这可能不应该是启动活动,因为其中一些活动将同时发送.

  • 怎么样?你能提供一些示例代码吗? (7认同)