Can accessibility service be stopped or killed?
Asked Answered
Z

2

4

I have written an accessibility service and I want the service to be stopped/killed once it receives an accessibility event (i.e. in method onAccessibilityEvent). Can this be done? I have tried to kill using kill process as below but it is not working.

unbindService(mSvcConn);  
android.os.Process.killProcess(android.os.Process.myPid());

I have given the below permission in AndroidManifest.xml

<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

I know that accessibility service cannot be started programmatically but can't it be stopped/killed either?

Zither answered 10/10, 2014 at 14:21 Comment(1)
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />Zither
H
6

An accessibility service can't remove itself from the list of enabled services or kill its own process without being automatically restarted, but you can call AccessibilityService.setServiceInfo(new AccessibilityServiceInfo()) to effectively turn off your service.

Calling AccessibilityManager.isEnabled() will still return true, but your service won't receive events.

Hath answered 13/10, 2014 at 21:36 Comment(2)
Thanks Alanv for the reply. Your reply seems to answer my question but my only concern is that the service is running when it is not doing any task. Also, when the phone is restarted, accessibility service seems to start again. Any idea as to how we can avoid restarting of this service when phone is restarted?Zither
You can prompt the user to disable the service. There are some hacky ways to actually fully disable the service (e.g. disable the component) but they'll cause more trouble than they are worth.Hath
G
3

In Android 7.0 (API 24), you can call disableSelf() on the AccessibilityService to disable the service, which also stops it.

Gq answered 7/11, 2016 at 20:25 Comment(2)
i am using it and it is not working. can you explain why?Abjuration
@MateenChaudhry disableSelf() does not work in lifecycle moment. It will not work when called during onCreate, but it will work when called in onServiceConnected.Sloat

© 2022 - 2024 — McMap. All rights reserved.