java NotificationListenerService。cancelNotification引发NullPointerException
我正在尝试从状态栏中取消/删除通知,方法是使用我的NotificationListenerService
中的cancelNotification()方法,代码如下:
public final void mcancelNotification(String pkgn, String t, int i){
cancelNotification(pkgn, t, i); //line 84
}
然而,我得到了一个NullPointerException
(见下文),在做了一些测试之后,我注意到tag t
是null
。
这就是我如何从onNotificationPosted
方法中获得pkgn
、t
和i
值的原因:
packageName = sbn.getPackageName();
tag = sbn.getTag();
id = sbn.getId();
如果标记等于null,如何删除通知?我错过什么了吗?非常感谢您的帮助,谢谢
NPE:
09-13 14:23:30.270: E/AndroidRuntime(29456): FATAL EXCEPTION: main
09-13 14:23:30.270: E/AndroidRuntime(29456): Process: com.project.now, PID: 29456
09-13 14:23:30.270: E/AndroidRuntime(29456): java.lang.NullPointerException
09-13 14:23:30.270: E/AndroidRuntime(29456): at 安卓.os.Parcel.readException(Parcel.java:1471)
09-13 14:23:30.270: E/AndroidRuntime(29456): at 安卓.os.Parcel.readException(Parcel.java:1419)
09-13 14:23:30.270: E/AndroidRuntime(29456): at 安卓.app.INotificationManager$Stub$Proxy.cancelNotificationFromListener(INotificationManager.java:469)
09-13 14:23:30.270: E/AndroidRuntime(29456): at 安卓.service.notification.NotificationListenerService.cancelNotification(NotificationListenerService.java:116)
09-13 14:23:30.270: E/AndroidRuntime(29456): at com.project.now.NoLiSes.mcancelNotification(NoLiSes.java:84)
# 1 楼答案
//取消以前显示的通知
//通知通知经理取消一个通知
链接http://developer.android.com/reference/android/app/NotificationManager.html#cancel%28java.lang.String,%20int%29
链接https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
在API级别1中添加
位按位或入标志字段,如果用户单击通知时应取消通知,则应设置该字段
//清除所有通知
但是你不能取消其他应用的通知,这是不可能的
# 2 楼答案
我找到了解决办法
首先,您必须只有一个NotificationListenerService实例。在我的应用程序中,我将以单音处理我的通知,并使用来自许多类的通知。这是我服务的一部分:
ServiceListener接口:
在NotificationsHolder类中(它是单音):
现在,无论我在哪里需要取消通知,我都会:
它起作用了