如何使用Notification.deleteIntent
我正在尝试检测我的通知何时被清除。我的问题直接引用了这个答案,它概述了我的想法去做。这就是我实现操作的方式:
// usual Notification initialization here
notification.deleteIntent = PendingIntent.getService(context, 0, new Intent(context, CleanUpIntent.class), 0);
notificationManager.notify(123, notification)
这是 CleanUpIntent 类:
class CleanUpIntent extends IntentService {
public CleanUpIntent() {
super("CleanUpIntent");
}
@Override
protected void onHandleIntent(Intent arg0) {
// clean up code
}
}
之后,我只是像平常一样启动通知,但当我去测试它时(按“清除所有通知”)什么也没有发生。我插入了一行代码,当 IntentService 启动时,该代码会向 LogCat 打印一些内容,但没有运行任何内容。这是我应该使用Notification.deleteIntent的方式吗?
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)
This is the CleanUpIntent class:
class CleanUpIntent extends IntentService {
public CleanUpIntent() {
super("CleanUpIntent");
}
@Override
protected void onHandleIntent(Intent arg0) {
// clean up code
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每当用户清除通知时都会调用的示例代码,希望对您有所帮助。
NotificationBroadcastReceiver.java
AndroidManifyingst.xml
sample code which will be called whenever user clears the notification, hope it will help you .
NotificationBroadcastReceiver.java
AndroidManifiest.xml
您需要做的是注册一个
BroadcastReceiver
(可能在 AndroidManifest.xml 中,或者在Service
中使用registerReceiver
),然后设置deleteIntent
是一个将被该接收者捕获的Intent
。What you need to do is register a
BroadcastReceiver
(probably in your AndroidManifest.xml or alternatively usingregisterReceiver
in aService
) and then setdeleteIntent
to be anIntent
that will be caught by that receiver.您应该使用 getBroadcast 方法而不是 getService ,并且应该为特定操作注册接收器。
You should use getBroadcast methode instead getService and should register receiver for specific Action.
不需要显式接收器。当按下clear按钮时,deleteIntent将被自动调用。
An explicit receiver is not required. deleteIntent will be called automatically while pressing the clear button.