Android uses Notificationlistenerservice to monitor the usage of various notification through the system

Source: Internet
Author: User
Tags call back

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.