NotificationListenerService:getActiveNotifications 上的 NullPointerException [英] NotificationListenerService: NullPointerException on getActiveNotifications
问题描述
我正在尝试根据本教程在我的应用程序中实现 NotificationListenerService:http://www.kpbird.com/2013/07/android-notificationlistenerservice.html,但我在调用 getActiveNotifications 时遇到 NullPointerException.
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.");
}
}
推荐答案
从那以后,我了解了更多关于这个的知识并开始工作!
注意:首先,请确保您已在 Android 设备的通知访问设置面板中启用您的应用.
直到现在我遇到了完全相同的问题.事实证明,覆盖 onBind
是危险的.如果确实覆盖了 onBind
,则必须返回 super.onBind(intent)
返回的相同 IBinder.如果您想返回自己的自定义绑定器,请确保使用唯一的 Intent,并且仅在收到自定义 Intent 时才返回您的自定义绑定器.
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);
}
}
一旦您授予它读取通知的权限,系统就会在您的服务上调用 onBind.如果您的 onBind 向系统返回自定义绑定器,系统将不会给您通知,并且可能导致空指针或安全异常.
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.
希望这有帮助!
这篇关于NotificationListenerService:getActiveNotifications 上的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!