AccessibilityService is started but does not receive AccessibilityEvents on JellyBean
Asked Answered
S

2

12

I have a AccessibilityService that shall read out any incoming notification. It does work fine with ICS and below, but stopped working with JB.

Below are the Manifest and the code:

<service
        android:name=".Services.InstantMessengerJb"
        android:enabled="@bool/is_post_jb"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        tools:ignore="ExportedService" >
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibility_service_config" />

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

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    SettingsClass.logMe(tag, "New event!");
    new AccessibilityProcessing(this, event);
}

@Override
protected void onServiceConnected() {
    if (isInit) {
        return;
    }
    SettingsClass.logMe(tag, "We are connected!");
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
    setServiceInfo(info);
    isInit = true;
}

As said before it does work on all preJB-Devices like a charm, however on JB the service starts (I get the "We are connected"), but not a single event is fired.

Is there anything wrong with the code?

Suomi answered 18/8, 2012 at 15:35 Comment(3)
I've found it works, but only if defined declarativley. You'll also need to add the new binding permission and have some conditional logic to determine if running in jelly bean otherwise older phones won't run with the new binding parameter. I'll try and get an example together.Collen
I noticed on some devices having the binding permission declared still works OK with ice cream sand, but not in the bread of ginger.Psychedelic
ahh.. well correction: bind accessibilty service is >= api 16, but looks like it is ignored and still works in api 15Psychedelic
C
18

This is the way I got it to work:

  1. Subclass the service class so there are 2 versions of it.

  2. Have a bools.xml file in the res/values directory and also one in the res/values-v16 directory

  3. Have a is_jelly_bean boolean set to true in v16 and a is_not_jelly_bean in the res/values directory version

  4. In the manifest have something like this

    <service android:name=".service.MainRunningService" android:enabled="@bool/is_jelly_bean"
        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>
    
    <service android:name=".service.MainRunningServicePreJellyBean" 
        android:enabled="@bool/is_not_jelly_bean">
        <intent-filter >
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
    
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibilityservice" />
    </service>
    

Have an accessibility service xml file in the res/xml directory call accessibilityservice.xml

In it have something like this:

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:description="@string/description_that_shows_on_the_accessibility_page" />
Collen answered 20/8, 2012 at 17:49 Comment(10)
Thanks for your answer! Please note that I already use a modified service for JB (see code), but I will try it with service description in the XML file and report back.Suomi
That's what I found made the difference. I've no idea why the code version just doesn't seem to work.Collen
This worked for me on Android 4.1.1, but failed on Android 4.2 without the intent-filter in the "jelly bean" version of the service declaration. Obviously you also have to add a value-v17 folder to account for the SDK change.Linzy
@RupertRawnsley I guess you've got your target sdk set to 17. At the moment I'm only using a target of 16 and it still works.Collen
@Collen According to Google, you should be able to do this setup in setServiceInfo()....does this not work well? I am having a similar problemPursley
@ECEsurfer - no sadly not in jelly bean. I'm sure it's a bug in androidCollen
To get this working, I had to add a <category> element to the is_jelly_bean <intent-filter>. I used TalkBack as an example, pulling the category from its sourceMarquesan
Nice workaround. There must be a better way, but for the moment, this works for me. (I'll point out that if the service is the same for each version, there seems to be no need to subclass it. For me, only the manifest differed from version to version.)Bennettbenni
I guess books.xml is a typo of bools.xml?Haihaida
Awesome man didn't expect to be able to fix this bad bug!!Lajuanalake
H
3

I experienced this problem in Android 4.1.1. The accessibility service didn't receive events until I disabled and re-enabled the accessibility service.

To fix the problem, I defined the accessibility service parameters in both setServiceInfo() and accessibilityservice.xml.

In my case, there was no need to define two different Android-version-dependent accessibility services.

Haihaida answered 12/6, 2015 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.