Android uses icationicationlistenerservice to listen to various notifications.
Icationicationlistenerservice is a service triggered by the system. When an application initiates a notification, the system calls back the Notification action and information to icationicationlistenerservice.
Before inheriting the NotificationListenerService service to implement its own logic, you must add the following code to the configuration file to obtain the permission.
In this way, you can find the switch to enable the Service in the system settings. Take miui as an example, in the settings-security and privacy-Notification read permission
The Service has the following two abstract methods that developers need to implement 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()); }}
That is to say, when the system finds that an application generates a notification or the user deletes a notification, the two functions of the Service will be called back. The StatusBarNotification parameter of the function contains the specific information of the notification.
In Android Wear development, this method is used to capture notifications from mobile phones and synchronize the notifications to watches.