Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

android - AccessibilityService is started but does not receive AccessibilityEvents on JellyBean

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...