Android uses icationicationlistenerservice to listen to various Notification usage methods. androidlistener

Source: Internet
Author: User

Android uses icationicationlistenerservice to listen to various Notification usage methods. androidlistener

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.

 <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, 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.


In android, how does one clear the system notification?

Notification notification = new Notification (R. drawable. icon,
"Ga ", System. currentTimeMillis ());

Icationicationmanager nm = (NotificationManager)
Context. getSystemService (Context. icationication_service );

Intent ii = new Intent ();

PendingIntent contentIntent = PendingIntent. getActivity (context, 0,
Ii, PendingIntent. FLAG_UPDATE_CURRENT );

Notification. flags = Notification. FLAG_AUTO_CANCEL;

Notification. defaults = Notification. DEFAULT_SOUND;

Intent I = new Intent ("com. android. broadcasttest. xxx"); // broadcast already exists

Notification. deleteIntent = PendingIntent. getBroadcast (this, 0, I, 0 );

Notification. setLatestEventInfo (context, "Ga message ",

String. format ("% d new messages in total", 1), contentIntent );

Nm. Y (R. string. app_name, notification );
This is an example.

How to display multiple Android notifications

Void android. app. icationicationmanager. Y (int id, Notification notification) Id should not be set to the same

Related Article

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.