John Smith John Smith - 1 year ago 197
Android Question

Android: Correct way to stop, unbind and completely remove NotificationListenerService

I have a NotificationListenerService that is started from the Manifest. The service works and is processing notifications. I want to be able to stop, unbind and remove (onDestroy called). Can someone provide a minimal example?

I've tried implementing ServiceConnection and unbind() and selfstop() but it still won't run onDestroy. I also tried to setup NotificationListenerService inline but couldn't figure out how to bind it.

Answer Source

As mentioned in the What's new with Notifications in Android N talk, a NotificationListenerService is bound to the system forever. Since they are always bound by the system, it will never be destroyed.

As of Android N, you'll be able to call requestUnbind() to stop listening and NotificationListenerService.requestRebind() to start again, but before that there is no specific way to cause the system to unbind from your NotificationListenerService.

You can, of course, disable the service entirely using code such as:

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName(getPackageName(),

Which will prevent the service from running entirely. Unfortunately this may have a side effect where the system temporarily blacklists your NotificationListenerService (as it repeatedly fails to bind to it), so should only be used if you are disabling it permanently/semi-permanently.