2

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?

2 Answers2

0

I encountered this problem 3 months ago, your service runs in a specified process android:process=":NotificationAccessibilityListener", that's why your accessibility service does not work on some devices, just remove this line:

<service android:name=".features.services.NotificationAccessibilityListener"
            android:label="@string/app_name"
            android:enabled="true"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">

            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />

</service>
rainash
  • 864
  • 6
  • 15
  • 1
    Sorry I took 22 hours to test this. Anyways I had high hopes for your solution to solve my problem :'), but it still persist, the accessibility service, even though is on, does not receive any events on phone, but currently do on emulator – Ivan Alburquerque Oct 01 '16 at 04:46
0

Had the same issue, was all over SO looking for answers .. (it worked before, then I removed stuff I thought was unnecessary ..

I was testing this on a samsung,

android:notificationTimeout="100"

made it work .. i had it at 10 (might be ok for a simulator / faster phone. but not a phone i had) ..

had to do this (might work / not ..)


        info.eventTypes = TYPE_WINDOW_STATE_CHANGED | TYPE_WINDOW_CONTENT_CHANGED | TYPE_VIEW_TEXT_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.notificationTimeout = 100;
        this.setServiceInfo(info);
John Ryan
  • 1
  • 1