In the Android app that I'm working on, I'd like to be able to detect when a new status bar notification appears, regardless of if it was caused by my app. To be more specific, I want to count the number of notifications in a given time frame.
Is this even possible, and if so, how?
Open the Settings app, or pull down on the notification shade and tap on the Settings icon (gear-shaped). Tap on Notifications. Tap on Notification history.
↳ android.service.notification.NotificationListenerService. A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.
You can manually disable the setup wizard on the device. From Android Setup > Apps & Notifications. Search for All apps and show System apps (hidden by default). Select and disable Android Setup.
Actually, it is possible, I use it in my app.
For Android 4.2 and below:
You need to register an AccessibilityService and make sure the user enables the service.
Example for a service:
public class InstantMessenger extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent event) { if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) { //Do something, eg getting packagename final String packagename = String.valueOf(event.getPackageName()); } } @Override protected void onServiceConnected() { if (isInit) { return; } AccessibilityServiceInfo info = new AccessibilityServiceInfo(); info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED; info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN; setServiceInfo(info); isInit = true; } @Override public void onInterrupt() { isInit = false; } }
Example for checking if your Service is activated
For Android 4.3 and above:
Use the Notification Listener API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With