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.service.notification.NotificationListenerService. A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.
You need to grant access to your app to read notifications: "Settings > Security > Notification access" and check your app.
To use NotificationListenerService, we need to create a java file which extends NotificationListenerService and implement two callback methods. Both methods have a parameter named “sbn”, which is an object of StatusBarNotification class. StatusBarNotification provides necessary information about Notifications.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With