ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

Android AccessibilityService Event not received ONLY in KitKat (4.4)

I have created Accessibility service in Android and it works perfectly on 4.2 (Jelly Bean) and 5.1 (Lollipop) but not on 4.4 (KitKat).

On KitKat, it doesn't receive ANY events and doesn't call onAccessibilityEvent at all.

Am I doing something wrong?
Here's my implementation:

accessibilityservice.xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeNotificationStateChanged"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:canRetrieveWindowContent="true"
    android:description="@string/app_name"
    android:notificationTimeout="100" />

myAccessibilityService.java

public class myAccessibilityService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent e) {
        if (e.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
             Log.d(TAG, "NOTIFICATION!!");
        }
    }

    @Override
    protected void onServiceConnected() {
        Log.d(TAG, AccessibilityService Connected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.flags = AccessibilityServiceInfo.DEFAULT;
        info.notificationTimeout = 100;
        setServiceInfo(info);
    }

    @Override
    public void onInterrupt() {
        Log.d(TAG, "myAccessibilityService onInterrupt Called");
    }

}

AndroidManifest.xml

    <service
        android:name=".myAccessibilityService"
        android:enabled="true"
        android:label="@string/app_name"
        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/accessibilityservice" />
    </service>

When I go to settings and turn the service on, onServiceConnected is called and it says it's on and running BUT it never called onAccessibilityEvent.

Like I said, it works on JellyBean AND Lollipop. So what could be the problem?

Upvotes: 0

Views: 1049

Answers (2)

Prasad Pawar
Prasad Pawar

Reputation: 1711

First of all, in onServiceConnected(), remove all the lines except the Log.

Your Accessibility service can miss events if you have any other accessibility service with same package name & registered for same events installed on the device.

Also,

1) Try to check other events like TYPE_WINDOW_CONTENT_CHANGED and see if KitKat device receives them.

2) Try running on a different version of KitKat (might be a version issue).

Upvotes: 0

MobA11y
MobA11y

Reputation: 18900

When you create a new service info, you remove a lot of the default properties of the service info. I recommend fetching the service info of the service, and then manipulating that object. I'm not convinced this will fix your problem, but it's a better idea in general.

To clarify, I recommend exchanging this line:

AccessibilityServiceInfo info = new AccessibilityServiceInfo();

With this:

AccessibilityServiceInfo info = this.getServiceInfo();

So that you maintain the default configuration from any XML properties, default construction, etc.

Upvotes: 1

Related Questions