I have a perfectly running application with an accessibility service. It has been working as expected so far. The problem came out at the moment when I tried to build a new updated version. Nothing has been changed in the code related to the accessibility service. But the service is no longer binding to the updated application. There is no errors or warnings in logs. Here is the part of the manifest.xml:
...
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<application
android:name=".AppState"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:allowBackup="true">
// ... several activities, services and bootup receiver goes here...
<service android:name=".ForegroundMonitor"
android:label="@string/foreground_monitor_label"
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/serviceconfig" />
</service>
</application>
And here is the serviceconfig.xml:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewClicked|typeViewFocused|typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="1000"
android:canRetrieveWindowContent="false"
android:description="@string/foreground_monitor_about"
/>
The accessibility service is registered according to the system, can be started or stopped, and the service settings get changed if I change them in the app and reinstall the app.
But the service is not binding to my app anymore. The onServiceConnected
method of the ForegroundMonitor
is not called.
I have found some similar questions here on SO:
- Accessibility Service for my app is not working if I reinstall my app - with an answer;
- Android accessibility service not connected after reinstall - without an answer;
I've tried the only suggested solution, but it does not work in my case.
According to my tests, the problem occurs only on Android 5.0.1. On Android 2.3.3 the app works normally, and the service is bound successfully.
Could someone share a solution for this issue, which is presumably an Android bug?
UPDATE
I have just found a workaround: it's sufficient to rename the class that extends AccessibilityService
(ForegroundMonitor in my case), for example by adding a versioning suffix. The drawback of this method is that users must reenable the new accessibility service in the system, because it is treated as another one. Ideally, update of an app should not require this if the service was once enabled.
So I'm still interested in a solution - more convenient solution.
Thanks in advance.