8

I am trying to implement an AccessibilityService. I have shared my code below. When I turn on my accessibility service from settings menu then onServiceConnected() is called but onAccessibiltyEvent() is not called at all. Please guide me on this.

Service Declaration in manifest file.

<service
            android:name=".MyAccessibilityService"
            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>

XML file

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:accessibilityEventTypes="typeAllMask"
    android:canRequestFilterKeyEvents="true"
    android:accessibilityFlags="flagDefault"

    android:notificationTimeout="100"
    android:canRetrieveWindowContent="true"
    />

MyAccessibiltyService.java

public class MyAccessibilityService extends AccessibilityService {


    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d(TAG,"Service Connected");

    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        System.out.println("Event Occurred");
        Log.d(TAG, "onAccessibilityEvent: event=" + event);
        AccessibilityNodeInfo nodeInfo = event.getSource();
        if (null == nodeInfo) return;


    }

    @Override
    public void onInterrupt() {
        Log.d(TAG,"Accessibility Interrupted" );

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"Service Destroyed");
    }
}

Please note that I have already checked all stackoverflow answers hence do not mark this as duplicate.

vipin agrahari
  • 1,973
  • 1
  • 17
  • 29

3 Answers3

9

I needed to add a feedback type to the accessibility_service_config file to make it work.

Try to add this

android:accessibilityFeedbackType="feedbackAllMask"

Tested on a Nexus 6P with Android 7.1.2

Asapha
  • 375
  • 4
  • 10
8

Everything in code seems correct.

Try one of these:

  1. Go to Settings > Accessibility > YourApp > Disable and then Enable.

  2. Reboot the device.

For me step 2 solved the problem.

Ashwin
  • 4,152
  • 33
  • 42
1

It seems your accessibility_service_config lacks packages which define the apps your AccessibilityService wants to monitor. Add android:packageNames="app.package.name" in accessibility_service_config.

You can refer to Android developer guide here.

Eidt:

Well it seems to mean monitoring all packages if you don't supply packages names. Maybe you can try to specify a package to see if it works.

shhp
  • 3,313
  • 2
  • 16
  • 23