有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NotificationListenerService。cancelNotification引发NullPointerException

我正在尝试从状态栏中取消/删除通知,方法是使用我的NotificationListenerService中的cancelNotification()方法,代码如下:

public final void mcancelNotification(String pkgn, String t, int i){
    cancelNotification(pkgn, t, i); //line 84
}

然而,我得到了一个NullPointerException(见下文),在做了一些测试之后,我注意到tag tnull。 这就是我如何从onNotificationPosted方法中获得pkgnti值的原因:

    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)

共 (2) 个答案

  1. # 1 楼答案

    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)
    

    链接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

    public static final int FLAG_AUTO_CANCEL
    

    在API级别1中添加

    位按位或入标志字段,如果用户单击通知时应取消通知,则应设置该字段

    //清除所有通知

    NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancelAll();
    

    但是你不能取消其他应用的通知,这是不可能的

  2. # 2 楼答案

    我找到了解决办法

    首先,您必须只有一个NotificationListenerService实例。在我的应用程序中,我将以单音处理我的通知,并使用来自许多类的通知。这是我服务的一部分:

        private ServiceListener listener;
        private NotificationsHolder holder;
    
        @Override
        public void onCreate() {
         super.onCreate();
         packageManager = getPackageManager();
         holder = NotificationsHolder.getInstance();
         listener = holder;
         listener.registerService(this);
        }
    

    ServiceListener接口:

        public interface ServiceListener {
         void registerService(NotificationService service);
         void unregisterService();
        }
    

    在NotificationsHolder类中(它是单音):

        @Override
        public void registerService(NotificationService service) {
         this.service = service;
        }
    
        @Override
        public void unregisterService() {
         service = null;
        }
    
        public NotificationService getService(){
         return service;
        }
    

    现在,无论我在哪里需要取消通知,我都会:

        holder = NotificationsHolder.getInstance();
        NotificationService service = holder.getService();
        service.cancelNotification (myNotification.getPackageName(), myNotification.getTag(), myNotification.getIndex());
    

    它起作用了