26

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

设置重复警报:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);

我的通知代码:

@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();

}

我知道我需要打电话alarmManager.cancel(displayIntent)来取消我的重复闹钟。但是,我不明白将这段代码放在哪里。仅当用户点击通知或将其关闭时,我才需要取消重复警报。谢谢你的帮助!

4

3 回答 3

23

我相信这Notification.deleteIntent就是您正在寻找的。医生说:

当通知被用户明确解除时执行的意图,无论是使用“全部清除”按钮还是单独将其滑开。这可能不应该启动一个活动,因为其中几个将同时发送。

于 2014-04-13T20:10:57.587 回答
2

对于所有那些未来的人——你可以注册一个广播接收器来监听通知删除inents。

创建一个新的广播接收器:

public class NotificationBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (action == null || !action.equals(Config.NotificationDeleteAction)) {
            return;
        }

        // Do some sweet stuff
        int x = 1;
    }
}

在您的应用程序类中注册广播接收器:

“如果您的应用程序以 API 级别 26 或更高级别为目标,则您不能使用清单为大多数隐式广播(不专门针对您的应用程序的广播)声明接收器。”

安卓文档。

  registerReceiver(
        new NotificationBroadcastReceiver(),
        new IntentFilter(Config.NotificationDeleteAction)
  );

您可能注意到静态变量Config.NotificationDeleteAction。这是通知的唯一字符串标识符。它通常遵循以下{namespace}{actionName}约定:

you.application.namespace.NOTIFICATION_DELETE

在通知生成器上设置删除意图:

notificationBuilder
       ...
        .setDeleteIntent(createDeleteIntent())
       ...

其中,createDeleteIntent是以下方法:

private PendingIntent createDeleteIntent() {
    Intent intent = new Intent();
    intent.setAction(Config.NotificationDeleteAction);

    return PendingIntent.getBroadcast(
            context,
            0,
            intent,
            PendingIntent.FLAG_ONE_SHOT
    );
}

当您的通知被解除时,您注册的广播接收器应该会收到意图。

于 2017-11-04T04:38:11.793 回答
0

您还可以使用 Activity PendingIntent,如果您有一个可以处理解雇的 Activity,这可能更容易实现,因为您不必创建和配置广播接收器。

public static final String DELETE_TAG = "DELETE_TAG";

private PendingIntent createDeleteIntent(Context context) {
    Intent intent = new Intent(context, MyActivity.class);
    intent.putExtra(DELETE_TAG, true);

    return PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_ONE_SHOT
    );
}

MyActivity 将在其 onCreate() 中接收意图,在此示例中,可以查找 DELETE_TAG 额外内容以识别它。

于 2018-04-19T04:44:08.247 回答