A good solution I've come up with is to run your AccessibilityService in a separate process. You can add an android:process
attribute in the manifest, e.g.
<service
android:name=".ExampleService"
android:process="com.example.service"
...
Now your service will be running in a separate process with the given name. From your app you can call
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
activityManager.runningAppProcesses.any { it.processName == "com.example.service" }
Which will return true
if the service is running and false
otherwise.
IMPORTANT: note that it will show you when your service was started, but when you disable it (meaning, after system unbinds from it) the process can still be alive. So you can simply force it's removal:
override fun onUnbind(intent: Intent?): Boolean {
stopSelf()
return super.onUnbind(intent)
}
override fun onDestroy() {
super.onDestroy()
killProcess(Process.myPid())
}
Then it works perfectly. I see this method more robust than reading values from settings, because it shows the exact thing needed: whether the service is running or not.