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