Android accessibility service backwards compatibility and jelly bean

不想你离开。 提交于 2020-01-03 02:19:07

问题


I've got an app that uses the accessibility service to monitor for events. It's always worked fine in versions of android up to ICS, but with Jelly bean I'm not having much luck.

As the documents mention I've added

android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"

to my service in the manifest file. This causes the app to work fine using the declarative xml method for the xml.

The problem is on backwards compatibility. For versions such as gingerbread I'm now getting the following error:

07-15 22:15:56.090: E/ACRA(1168): Caused by: java.lang.SecurityException: Not allowed to start service Intent { cmp=com.example/.MainRunningService (has extras) } without permission android.permission.BIND_ACCESSIBILITY_SERVICE

I've updated to the latest revision of the compatibility jar hoping that may help, but still get the error.

I'm not sure how I get compatibility between older and newer versions.

If I remove the BIND_ACCESSIBILITY_SERVICE from the manifest, then my app doesn't show in Jelly Bean to be able to switch accessibility on. Any suggestions?


回答1:


ok, mark murphy suggested a few bits to me and this is what worked.

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

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

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

Then 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>


来源:https://stackoverflow.com/questions/11496140/android-accessibility-service-backwards-compatibility-and-jelly-bean

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