3

How can I react on notification dismiss? I need to kill my background process when a certain notification is cancelled.

In this thread it says use a broadcast receiver, but there is no intent-filter for notifications

Notification deleteIntent does not work

4

1 回答 1

8

如果您定义意图来手动触发您的类,则不需要意图过滤器来获得广播。

只需实现 BroadcastReceiver

public class NotificationDeleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // kill service here
    }
}

并将 Receiver 添加到 AndroidManifest.xml

<receiver android:name="NotificationDeleteReceiver" />

然后将 PendingIndent 设置为通知:

notification.deleteIntent = PendingIntent.getBroadcast(
    this, 0, new Intent(context, NotificationDeleteReceiver.class), 0);

这将起作用。

于 2013-09-07T12:19:16.270 回答