8

I have enabled service in my AndroidManifest.xml. And I want to disable it from my code:

getPackageManager().setComponentEnabledSetting(
    serviceName, 
    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
    PackageManager.DONT_KILL_APP);

If I disable the service like above, will it be disabled forever? Or not?

shmakova
  • 6,076
  • 3
  • 28
  • 44

2 Answers2

10

Yes, settings are persistent until reinstall (delete application, install again) or new value is set again with setComponentEnabledSetting(). Additionally, as a border case, someone with super-user privileges can reset your app.

Please read this answer for details.

Siegmeyer
  • 4,312
  • 6
  • 26
  • 43
7

It will be disabled until you enable it or the user uninstalls your app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The `setComponentEnabledSetting` can be used only for the current app, right? It can't enable/disable anything of other apps, right? – android developer Jan 05 '22 at 13:44
  • 1
    @androiddeveloper: Correct on both counts, barring some glaring security flaw. I also cannot rule out the possibility that device owner apps might have extra capabilities here. Plus, while a rooted app would not necessarily be able to simply call that method and affect other apps, they may be able to accomplish the same thing by other means. – CommonsWare Jan 05 '22 at 14:17
  • Thank you. You probably mean by the same means of adb? I wonder what's the command via adb. Do you know? I remember there are (or were) apps to control what's starting on boot (which I think it's a bad idea, usually). Do you know what they use? I haven't even tried them. – android developer Jan 05 '22 at 19:13
  • 1
    @androiddeveloper: "You probably mean by the same means of adb?" -- sorry, I do not know what options `adb` might offer here. "Do you know what they use?" -- no, sorry. – CommonsWare Jan 05 '22 at 20:25