如何使用Notification.deleteIntent如何使用、Notification、deleteIntent

2023-09-13 00:01:53 作者:妞,别以为爷不敢动你

我想检测时,我通知被清除。我的问题直接涉及此answer其中概述了我该做的。这就是我采取的行动:

I'm trying to detect when my notification gets cleared. My question directly refers to this answer which outlines what I'm suppose to do. This is how I'm implementing the actions:

// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)

这是CleanUpIntent类:

This is the CleanUpIntent class:

class CleanUpIntent extends IntentService {
    public CleanUpIntent() {
        super("CleanUpIntent");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        // clean up code
    }
}

之后,我只需启动该通知像我通常会,但是当我去测试它(pressing清除通知)没有任何反应。我插线$ C $的c表示绝版的东西,当IntentService被开始的logcat,但从来都没有跑。这是如何我假设使用Notification.deleteIntent?

Afterwards, I simply launch the notification like I normally would but when I go to test it out (pressing "Clear All Notifications") nothing happens. I inserted a line of code that out print something to LogCat when the IntentService gets started, but nothing ever ran. Is this how I'm suppose to use Notification.deleteIntent?

推荐答案

您需要做的是注册的BroadcastReceiver (很可能在你的Andr​​oidManifest.xml或者选择使用 registerReceiver 服务),然后设置 deleteIntent 是一个意图将捕获的接收器。

What you need to do is register a BroadcastReceiver (probably in your AndroidManifest.xml or alternatively using registerReceiver in a Service) and then set deleteIntent to be an Intent that will be caught by that receiver.