onAccessibilityEvent not called at all
Asked Answered
A

5

13

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.

Apostate answered 9/11, 2016 at 9:1 Comment(0)
O
13

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

Oration answered 2/4, 2017 at 14:54 Comment(0)
V
12

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.

Viveca answered 12/11, 2018 at 8:36 Comment(0)
K
2

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.

Kepi answered 9/11, 2016 at 9:36 Comment(1)
Yep Tried. No luck with that as well.Apostate
L
1

For some devices power consumption limitation, AccessibilityService won't work properly, for example:vivo,you can try these: Settings->Battery->Background power consumption managemwnt->the app->Hight background power usage.

It works for me.

Lyell answered 16/3, 2022 at 3:28 Comment(0)
Y
0

In my case, I didn't realize I needed to both turn on talkback on the phone and then grant talkback access to my app/service.

Younts answered 29/8, 2023 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.