NotificationListenerService:NullPointerException异常上getActiveNotifications [英] NotificationListenerService: NullPointerException on getActiveNotifications

查看:665
本文介绍了NotificationListenerService:NullPointerException异常上getActiveNotifications的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照这个教程来实现我的应用程序的NotificationListenerService: http://www.kpbird.com/2013/07/android-notificationlistenerservice.html ,但我有一个NullPointerException异常调用getActiveNotifications时。

 产生的原因:显示java.lang.NullPointerException
在android.os.Parcel.readException(Parcel.java:1437)
在android.os.Parcel.readException(Parcel.java:1385)


在android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:500)
在android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
在com.rootsoft.rsnotificationservice.RSNotificationService.activeNot(RSNotificationService.java:85)
在com.rootsoft.rsnotificationservice.RSNotificationService.access $ 0(RSNotificationService.java:81)
在com.rootsoft.rsnotificationservice.RSNotificationService $ 1.onReceive(RSNotificationService.java:105)
在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:763)
... 9个
 

我发送一个广播的服务,应产生的所有通知的清单:

 私人无效activeNot(){
    表L =新名单();
    l.Initialize();

    对于(StatusBarNotification SBN:getActiveNotifications()){< ----错误发生在这里
        l.Add(SBN);
    }

    Log.i(B4A,列表创建的。);


    }
}
 

解决方案

编辑:!因为我已经更多地了解了这一点,并把它工作

注意:首先,确保你已经启用了Android设备的通知访问设置面板中你的应用程序

我有相同的问题,到现在为止。原来,覆盖 onBind 是危险的。如果覆盖 onBind ,你必须返回相同的IBinder的 super.onBind(意向)的回报。如果你想返回自己的定制绑定,请确保您使用一个唯一的意图,并且只接受定制的意图时,返回的定制绑定。

  @覆盖
公众的IBinder onBind(意向意图)
{
    如果(intent.getAction()。等于(custom_intent))
    {
        返回customBinder;
    }
    其他
    {
        返回super.onBind(意向);
    }
}
 

该系统调用onBind您的服务,一旦你已经授予其权限读取通知。如果你的onBind返回一个定制绑定到系统中,系统不会给你的通知,并可能导致空指针或安全例外。

希望这有助于!

I'm trying to implement the NotificationListenerService in my application according to this tutorial: http://www.kpbird.com/2013/07/android-notificationlistenerservice.html ,but i'm having a NullPointerException when calling getActiveNotifications.

Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1437)
at android.os.Parcel.readException(Parcel.java:1385)


at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:500)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
at com.rootsoft.rsnotificationservice.RSNotificationService.activeNot(RSNotificationService.java:85)
at com.rootsoft.rsnotificationservice.RSNotificationService.access$0(RSNotificationService.java:81)
at com.rootsoft.rsnotificationservice.RSNotificationService$1.onReceive(RSNotificationService.java:105)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
... 9 more

I'm sending a broadcast to the service which should generate a list of all Notifications:

private void activeNot () {
    List l = new List();
    l.Initialize();

    for (StatusBarNotification sbn : getActiveNotifications() ) { <---- Error happens here
        l.Add(sbn);
    }

    Log.i("B4A", "List created.");


    }
}

解决方案

EDIT: I have since learned more about this and got it working!

NOTE: First, make sure you have enabled your app in the Notification Access settings pane of your Android device.

I had the exact same problem until now. Turns out, overriding onBind is dangerous. If you do override onBind, you have to return the same IBinder that super.onBind(intent) returns. If you want to return your own custom binder, make sure you use a unique intent, and only return your custom binder when the custom intent is received.

@Override
public IBinder onBind(Intent intent)
{
    if (intent.getAction().equals("custom_intent"))
    {
        return customBinder;
    }
    else
    {
        return super.onBind(intent);
    }
}

The system calls onBind on your service, once you have granted it permission to read Notifications. If your onBind returns a custom binder to the system, the system will not give you the notifications, and could lead to Null Pointer or Security Exceptions.

Hope this helped!

这篇关于NotificationListenerService:NullPointerException异常上getActiveNotifications的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆