Search code examples
javaandroidnotificationsstatusbarnosuchmethoderror

java.lang.NoSuchMethodError: android.service.notification.StatusBarNotification.getKey


I have a Service that extends NotificationListenerService. In the onNotificationPosted method, I am trying to get the getKey() function from the StatusBarNotification object that is being received.

All other functions work but when I do sbn.getKey(), I get the error below (logCat output below). Is the API incorrectly documented?

This is in Android API Level 18 (4.3)

public void onNotificationPosted(StatusBarNotification sbn) {

    Log.v(TAG, "**********  onNotificationPosted, in NLService.java");
    Log.v(TAG,
            "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText
                    + "\t" + sbn.getPackageName());

    Log.v(Constants.TAG_ALL, "------------------------- sbn.getId() = "
            + String.valueOf(sbn.getId()));
    Log.v(Constants.TAG_ALL, "------------------------- sbn.getKey() = "
     + sbn.getKey());
    Log.v(Constants.TAG_ALL,
            "------------------------- sbn.getPackageName() = "
                    + sbn.getPackageName());
    Log.v(Constants.TAG_ALL,
            "------------------------- sbn.getPostTime() = "
                    + String.valueOf(sbn.getPostTime()));
    Log.v(Constants.TAG_ALL, "------------------------- sbn.getUserId() = "
            + String.valueOf(sbn.getUserId()));
    Log.v(Constants.TAG_ALL,
            "------------------------- sbn.isClearable() = "
                    + String.valueOf(sbn.isClearable()));
    Log.v(Constants.TAG_ALL, "------------------------- sbn.toString() = "
            + String.valueOf(sbn.toString()));
}

LogCat output:

09-10 03:13:44.588: E/JavaBinder(7329): *** Uncaught remote exception!  (Exceptions are not yet   supported across processes.)
09-10 03:13:44.588: E/JavaBinder(7329): java.lang.NoSuchMethodError:     android.service.notification.StatusBarNotification.getKey
09-10 03:13:44.588: E/JavaBinder(7329):     at com.mavdev.focusoutfacebook.notifications.NLService.onNotificationPosted(NLService.java:133)
09-10 03:13:44.588: E/JavaBinder(7329):     at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationP osted(NotificationListenerService.java:167)
09-10 03:13:44.588: E/JavaBinder(7329):     at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
09-10 03:13:44.588: E/JavaBinder(7329):     at android.os.Binder.execTransact(Binder.java:388)
09-10 03:13:44.588: E/JavaBinder(7329):     at dalvik.system.NativeStart.run(Native Method)
09-10 03:13:44.588: W/dalvikvm(7329): threadid=9: thread exiting with uncaught exception (group=0xa4c09648)
09-10 03:13:44.588: E/AndroidRuntime(7329): FATAL EXCEPTION: Binder_2
09-10 03:13:44.588: E/AndroidRuntime(7329): java.lang.NoSuchMethodError: android.service.notification.StatusBarNotification.getKey
09-10 03:13:44.588: E/AndroidRuntime(7329):     at com.mavdev.focusoutfacebook.notifications.NLService.onNotificationPosted(NLService.java:133)
09-10 03:13:44.588: E/AndroidRuntime(7329):     at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:167)
09-10 03:13:44.588: E/AndroidRuntime(7329):     at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:56)
09-10 03:13:44.588: E/AndroidRuntime(7329):     at android.os.Binder.execTransact(Binder.java:388)
09-10 03:13:44.588: E/AndroidRuntime(7329):     at dalvik.system.NativeStart.run(Native Method)

Solution

  • The method #getKey() is added in API level 20 therefore you cannot use it in API level 18.

    See the documentation at the top the required API level is stated.