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, you need to add the following code in the configuration file to get the permissions.
<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 the developer 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 parameter statusbarnotification of the function contains specific information about the notification.
If it is in the Android Wear development, use this method to capture the phone's notifications and then sync to the watch, that is, using the service for transit