AccessibilityService is started but does not receive AccessibilityEvents on JellyBean

断了今生、忘了曾经 提交于 2019-11-26 19:38:25

问题


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?


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/12019848/accessibilityservice-is-started-but-does-not-receive-accessibilityevents-on-jell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!