This is a “bug”, although, as you said, this method should not be called from the NotificationListenerService . You should call it regular Activty or Service . I will explain later why.
In your wake, the following situation.
android.app.AppOpsManager.checkOpNoThrow (AppOpsManager.java:1536)
public int checkOpNoThrow(int op, int uid, String packageName) { try { return mService.checkOperation(op, uid, packageName);
Here he tries to catch RemoteException , although in Parcel ( android.os.Parcel.readException (Parcel.java:1599) ) this happens:
public final void readException(int code, String msg) { switch (code) { case EX_SECURITY: throw new SecurityException(msg); // 1599 case EX_BAD_PARCELABLE: throw new BadParcelableException(msg); case EX_ILLEGAL_ARGUMENT: throw new IllegalArgumentException(msg); case EX_NULL_POINTER: throw new NullPointerException(msg); case EX_ILLEGAL_STATE: throw new IllegalStateException(msg); case EX_NETWORK_MAIN_THREAD: throw new NetworkOnMainThreadException(); case EX_UNSUPPORTED_OPERATION: throw new UnsupportedOperationException(msg); } throw new RuntimeException("Unknown exception code: " + code + " msg " + msg); }
It throws a SecurityException .
As SecurityException NOT RemoteException try-catch fails to catch the exception in checkOpNoThrow , and you get an error message.
Moreover, if you try to use the application context (for example, this.getApplicationContext() ) instead of the listener service context, this will also result in an error. This is due to canWrite checking caller ID. The notification listener has a different UID from other parts of your application.
public static boolean canWrite(Context context) { int uid = Binder.getCallingUid(); return isCallingPackageAllowedToWriteSettings(context, uid, getPackageNameForUid( context, uid), false); }
EDIT
This error is tracked on tracking AOSP issues .