I am struggling with NotificationListenerService without much luck. I tried many recipes found here and there, especially on so ... But it works completely inconsistently.
Check out the code first:
Manifesto:
<service android:name=".services.NLService" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" > <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service>
Then the service itself:
public class NLService extends NotificationListenerService { private String TAG = "NLService"; // bind and unbind seems to make it work with Android 6... // but is never called with Android 4.4... @Override public IBinder onBind(Intent mIntent) { IBinder mIBinder = super.onBind(mIntent); Log.i(TAG, "onBind"); return mIBinder; } @Override public boolean onUnbind(Intent mIntent) { boolean mOnUnbind = super.onUnbind(mIntent); Log.i(TAG, "onUnbind"); isNotificationAccessEnabled = false; try { } catch (Exception e) { Log.e(TAG, "Error during unbind", e); } return mOnUnbind; } // onCreate is called with Android 4.4 // because the service is explicitly started from the MainActivity. // Not on Android 6 where the system binds this service itself... @Override public void onCreate() { super.onCreate(); Log.i(TAG, "********** onCreate"); @Override public void onNotificationPosted(StatusBarNotification sbn) { Log.i(TAG, "********** onNotificationPosted"); Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName()); } @Override public void onNotificationRemoved(StatusBarNotification sbn) { Log.i(TAG, "********** onNOtificationRemoved"); Log.i(TAG, "ID :" + sbn.getId() + "\t" + sbn.getNotification().tickerText + "\t" + sbn.getPackageName()); } }
And in the main event the service is launched or we will ask the user to enable the setting, if necessary:
if (!Utilities.hasNotificationAccess(this)) { Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); startActivity(intent); Log.i(TAG,"hasNotificationAccess NO"); } else { Log.i(TAG,"hasNotificationAccess YES"); // without this startService, it never works with Android 4.4... // but this is not needed in Android 6... Intent mServiceIntent = new Intent(this, NLService.class); startService(mServiceIntent); }
Obviously, access to the protected settings for notifications is enabled ...
On Android 6:
In NLService itself, adding the onBind and onUnbind methods makes it work, I can write the onBind log, and the onNotificationPosted function starts beautifully. But it lasts until the next launch of the application, and then no longer binds, no more onNotificationPosted, just nothing happens until I return to the security settings to uncheck the check for access to notifications: repeating this every time the application is launched, is not something possible in a production environment.
In Android 4.4:
Unlike Android 6, in MainActivity I need to explicitly launch NLService, otherwise it will never be installed by itself. But again, it works for the first start and never works again until you cancel the verification system check ...
Am I missing something obvious?