I have been hitting a wall with this problem for some time:
This Accesibility Service reads notifications and process them. At first it worked succesfully on my phone (Moto G4) but then it wouldn't work on any smartphone, even tho this very same code works on the Emulator 100%.
I have the app's accessibility service turned on, and I tend to deactivate it and activate it again just in case on some test runs, I have rebooted the phone and do a complete install of the app plus activating the service in the system settings.
The thing that perplexed me is that, as mentioned above, this code works flawlessy for emulator and it did previously on my phone. The AccesibilityService just don't receive any events on some smartphones.
Tested on Moto G4, LG G2, Asus, Huawei... running Marshmallow / Lollipop (not working)
Tested on emulators API 16-19-23 and they all work as expected
public class NotificationAccessibilityListener extends AccessibilityService {
private AccessibilityServiceInfo info = new AccessibilityServiceInfo();
private static final String TAG = "NotificationAccListener";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
final int eventType = event.getEventType();
if (eventType == TYPE_NOTIFICATION_STATE_CHANGED)
{
handleNotificationEvent(event);
}
else if (eventType == TYPE_WINDOW_STATE_CHANGED)
{
handleWindowStateEvent(event);
}
else {
Utils.log("onAccessibilityEvent -> Got un-handled Event");
}
}
@Override
public void onInterrupt() {}
@Override
public void onServiceConnected() {
// Set the type of events that this service wants to listen to.
// Others won't be passed to this service.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
info.eventTypes = TYPE_NOTIFICATION_STATE_CHANGED;
} else {
info.eventTypes = TYPE_NOTIFICATION_STATE_CHANGED | TYPE_WINDOW_STATE_CHANGED;
}
// Set the type of feedback your service will provide.
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
// Default services are invoked only if no package-specific ones are present
// for the type of AccessibilityEvent generated. This service *is*
// application-specific, so the flag isn't necessary. If this was a
// general-purpose service, it would be worth considering setting the
// DEFAULT flag.
// info.flags = AccessibilityServiceInfo.DEFAULT;
info.notificationTimeout = 20;
this.setServiceInfo(info);
}
...
}
This is the declaration in the AndroidManifest.xml
<service android:name=".features.services.NotificationAccessibilityListener"
android:label="@string/app_name"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:process=":NotificationAccessibilityListener">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
The Accessibility service configuration xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="20"
android:canRetrieveWindowContent="true"
/>
Why is the AccessibilityService not receiving any events anymore?