我尝试使用 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 android.os.Parcel.readException(Parcel.java:1471)
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.os.Parcel.readException(Parcel.java:1419)
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.app.INotificationManager$Stub$Proxy.cancelNotificationFromListener(INotificationManager.java:469)
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.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)
最佳答案
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
//取消之前显示的通知。
cancel(String tag, int id)
//通知通知管理器有关单个通知的驳回。
cancelNotification(String pkg, String tag, int id)
public static final int FLAG_AUTO_CANCEL
在 API 级别 1 中添加
如果用户单击通知时应取消通知,则按位或位将其设置到应设置的标志字段中。
//清除所有通知
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
但您无法取消其他应用程序的通知,这是不可能的。
关于java - NotificationListenerService.cancelNotification 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25823217/