SoFunction
Updated on 2025-03-11

Android uses NotificationListenerService to listen for notification bar messages

The NotificationListenerService is a service initiated by the system. When the application initiates a notification, the system will call back the notification application, action and information to the NotificationListenerService. However, before using it, you need to guide the user to authorize it. Using NotificationListenerService generally requires the following three steps.

Register Service

First, you need to register the service.

<service
  android:name=".NotificationCollectorService"
  android:label="@string/app_name"
  android:permission=".BIND_NOTIFICATION_LISTENER_SERVICE">
  <intent-filter>
    <action android:name="" />
  </intent-filter>
</service>

Inheritance to implement NotificationListenerService

Implement a service that inherits the NotificationListenerService and complete the operations you need in the onNotificationPosted.

public class NotificationCollectorService extends NotificationListenerService {
  @Override
  public void onNotificationPosted(StatusBarNotification sbn) {
    ("xiaolong", "open" + "-----" + ());
    ("xiaolong", "open" + "------" + ().tickerText);
    ("xiaolong", "open" + "-----" + ().(""));
    ("xiaolong", "open" + "-----" + ().(""));
  }

  @Override
  public void onNotificationRemoved(StatusBarNotification sbn) {
    ("xiaolong", "remove" + "-----" + ());

  }
}

Guide users to authorize

Since this service requires user authorization manually, the user needs to be booted before use.

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    String string = (getContentResolver(),
        "enabled_notification_listeners");
    if (!(())) {
      startActivity(new Intent(
          ".ACTION_NOTIFICATION_LISTENER_SETTINGS"));
    }
  }
}

After the user authorizes, he can monitor all information in the notification column.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.