NotificationListenerService:getActiveNotifications上的NullPointerException

时间:2013-09-08 14:17:58

标签: java android notifications

我正在尝试根据本教程在我的应用程序中实现NotificationListenerService:http://www.kpbird.com/2013/07/android-notificationlistenerservice.html,但是在调用getActiveNotifications时我遇到了NullPointerException。

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

我正在向服务发送广播,该广播应生成所有通知的列表:

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

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

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


    }
}

3 个答案:

答案 0 :(得分:16)

编辑:我已经了解了更多相关内容并使其正常运行!

注意:首先,请确保您已在Android设备的“通知访问权限”设置窗格中启用了应用。

到目前为止,我遇到了完全相同的问题。事实证明,覆盖onBind是危险的。如果覆盖onBind,则必须返回super.onBind(intent)返回的相同IBinder。如果要返回自己的自定义绑定器,请确保使用唯一的意图,并仅在收到自定义意图时返回自定义绑定器。

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

一旦您授予了读取通知的权限,系统就会对您的服务调用onBind。如果你的onBind向系统返回一个自定义绑定器,系统将不会给你通知,并可能导致空指针或安全例外。

希望这有帮助!

答案 1 :(得分:3)

不要直接调用onCreate或onBind中的getActiveNotification方法。因为onBind会调用super.onBind来初始化,所以你可以使用处理程序来替换。 这是我的演示: https://github.com/yihongyuelan/NotificationListenerServiceDemo

答案 2 :(得分:2)

当我尝试使用startService()启动服务时,发生了这种情况。我错了!当用户启用您的应用程序以侦听通知时,系统会为您执行此操作