Verhelst
Verhelst

Reputation: 1503

NotificationListenerService: NullPointerException on getActiveNotifications

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.");


    }
}

Upvotes: 11

Views: 6098

Answers (3)

Seven
Seven

Reputation: 197

Don't call the getActiveNotification method in onCreate or onBind directly. Because the onBind will call the super.onBind to initialize, so you can use the handler to replace. Here is my Demo: https://github.com/yihongyuelan/NotificationListenerServiceDemo

Upvotes: 3

demonslayer319
demonslayer319

Reputation: 946

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!

Upvotes: 20

stepic
stepic

Reputation: 693

It happened to me when I tried to start the service with startService(). I was wrong! The system does it for you when the user enables your app to listen for notifications

Upvotes: 2

Related Questions