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?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
pradeep
  • 45
  • 1
  • 5

2 Answers2

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
alanv
  • 23,966
  • 4
  • 93
  • 80
  • 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? – pradeep Oct 14 '14 at 07:32
  • 1
    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. – alanv Oct 14 '14 at 20:49
3

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

Sam
  • 40,644
  • 36
  • 176
  • 219
  • i am using it and it is not working. can you explain why? – Mateen Chaudhry Sep 03 '18 at 08:40
  • 1
    @MateenChaudhry `disableSelf()` does not work in lifecycle moment. It will not work when called during `onCreate`, but it will work when called in `onServiceConnected`. – darken Jun 25 '23 at 07:58