11

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?

Force
  • 6,312
  • 7
  • 54
  • 85
  • 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. – Andrew Aug 20 '12 at 17:31
  • I noticed on some devices having the binding permission declared still works OK with ice cream sand, but not in the bread of ginger. – logray Mar 02 '13 at 00:34
  • ahh.. well correction: bind accessibilty service is >= api 16, but looks like it is ignored and still works in api 15 – logray Mar 02 '13 at 05:13

2 Answers2

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" />
Jug6ernaut
  • 8,219
  • 2
  • 26
  • 26
Andrew
  • 7,548
  • 7
  • 50
  • 72
  • 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. – Force Aug 20 '12 at 18:58
  • 1
    That's what I found made the difference. I've no idea why the code version just doesn't seem to work. – Andrew Aug 20 '12 at 22:12
  • 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. – Rupert Rawnsley Nov 15 '12 at 06:50
  • 1
    @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. – Andrew Nov 15 '12 at 10:23
  • @Andrew According to Google, you should be able to do this setup in setServiceInfo()....does this not work well? I am having a similar problem – onetwopunch Jan 18 '13 at 23:06
  • 1
    @ECEsurfer - no sadly not in jelly bean. I'm sure it's a bug in android – Andrew Jan 19 '13 at 00:10
  • 2
    To get this working, I had to add a `` element to the is_jelly_bean ``. I used TalkBack as an example, pulling the category from [its source](https://code.google.com/p/eyes-free/source/browse/trunk/accessibilityServices/talkback/AndroidManifest.xml#67) – benizi Aug 08 '13 at 12:37
  • 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.) – Novak Aug 22 '14 at 19:45
  • I guess `books.xml` is a typo of `bools.xml`? – Sam Jun 12 '15 at 08:59
  • Awesome man didn't expect to be able to fix this bad bug!! – HiDd3N Jan 14 '21 at 22:22
2

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.

Sam
  • 40,644
  • 36
  • 176
  • 219