Notificationlistenerservice is a service that is tuned through the system, and when an application initiates notification, the system will call back the action and information of the notification to Notificationlistenerservice.
Before inheriting the Notificationlistenerservice service to implement its own logic, it is necessary to include the following code in the configuration file for permission.
<service android:name= ". Notificationlistener " android:label=" @string/service_name " android:permission=" Android.permission.BIND _notification_listener_service "> <intent-filter> <action android:name=" Android.service.notification.NotificationListenerService "/> </intent-filter> </service>
In this way, the switch to turn on the service can be found in the system setup, taking MIUI as an example, in Settings-Security and privacy-Notification Read permissions
There are two abstract methods in the service that need to be implemented by developers when using the service.
public class Notificationcollectorservice extends Notificationlistenerservice { @Override public Void onnotificationposted (statusbarnotification sbn) { log.i ("ZPF", "open" + "-----" +sbn.tostring ()); } @Override public void onnotificationremoved (Statusbarnotification sbn) { log.i ("ZPF", "Shut" + "-----" + Sbn.tostring ());} }
This means that when the system discovers that an application generates a notification or the user deletes a notification, the service's two functions are called back, and the function's statusbarnotification includes the details of the notification.
The assumption is that in Android Wear development, this method captures the phone's notifications and then synchronizes them to the watch, which is a transit using the service
Android uses Notificationlistenerservice to monitor the usage of various notification through the system