我们如何检测通知取消?

Mar*_*ies 3 notifications android

我如何应对通知解雇?当某个通知被取消时,我需要终止我的后台进程.

在这个帖子中,它说使用广播接收器,但是没有用于通知的intent-filter

通知deleteIntent不起作用

flx*_*flx 8

如果您定义Intent手动触发您的类,则不需要使用intent-filter来获取广播.

只需实现BroadcastReceiver

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

并将Receiver添加到AndroidManifest.xml

<receiver android:name="NotificationDeleteReceiver" />
Run Code Online (Sandbox Code Playgroud)

然后将PendingIndent设置为通知:

notification.deleteIntent = PendingIntent.getBroadcast(
    this, 0, new Intent(context, NotificationDeleteReceiver.class), 0);
Run Code Online (Sandbox Code Playgroud)

这会奏效.