3

In Android it is possible to create a Service to do background tasks, etc by creating a subclass of Service. It order to use the Service it must be specified in the manifest for the app:

All services must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.

One of the parameters for a Service in the manifest is the 'enabled' option:

Whether or not the service can be instantiated by the system — "true" if it can be, and "false" if not.

What is the purpose in declaring a Service to be disabled - if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?

The only use I can see for disabling a Service in the manifest, and it seems of limited value, is if it's a Service used only for debugging, and I want it disabled for production. Am I missing something?

rene
  • 41,474
  • 78
  • 114
  • 152
Eborbob
  • 1,905
  • 1
  • 15
  • 30

2 Answers2

6

The android:enabled attribute set to a boolean value defined in a resource file. The purpose of this attribute is to enable or disable the service on devices running on different Android OS version.

For example, to disable the service on devices running Android 4.3 or lower include menifest attribute android:enabled="@bool/atLeastKitKat".

In addition to including this attribute in the manifest, you need to do the following:

In your bool.xml resources file under res/values/, add this line:

<bool name="atLeastKitKat">false</bool>

In your bool.xml resources file under res/values-v19/, add this line:

<bool name="atLeastKitKat">true</bool>
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
1

if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?

In the very specific case of a Service, I agree that it would be rare for you to want to disable it. One possibility would be for a service that plugs into the system (e.g., input method editor, accessibility service) that you only want to enable at runtime (via PackageManager and setComponentEnabledSetting()) if the user make an in-app purchase that unlocks the feature. I am sure that there are other Service scenarios for this, though none are leaping to mind at this early hour of the day (yawn!).

However, I suspect that Service "inherits" its android:enabled setting by virtue of being one of the Android component types, along with activities, providers, and receivers. Other scenarios for android:enabled will be a bit more common with other component types. For example, it is considered good form to have your BOOT_COMPLETED receiver be disabled until you know that you need it. So, for example, if the BOOT_COMPLETED receiver is only used to resume a download interrupted by a reboot, you only need that receiver enabled if you are doing a download. At all other times, you may as well leave it disabled, so you don't waste the user's time during "normal" reboots.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Good to know about the `PackageManager` `setComponentEnabledSetting()` method - it can be used to enable a service that is disabled in the manifest. I wouldn't have known where to find that (had I even considered it was possible!). – Eborbob Jun 18 '15 at 18:31
  • It seems that setComponentEnabledSetting can be used on accessibility service too, but only to disable it (and even then, it occurs only a few seconds after you did it). After that, even if it seems that it got enabled, it's not, as it doesn't get a call to onAccessibilityEvent. How come? Isn't there a way to enable/disable such a service (after user has enabled it, of course) ? – android developer Jan 21 '17 at 21:47
  • @androiddeveloper: I have never tried disabling a service that is already tied into the system through Settings, let alone re-enabling it thereafter. The scenario in this answer was where the app ships with the service disabled, then enables it (presumably permanently) based on an in-app purchase. Your scenario seems like a corner case that certainly is not well-documented and may not even be well-tested. Hence, as the saying goes, YMMV. – CommonsWare Jan 21 '17 at 21:55
  • @CommonsWare I see. I've created a question about it here, then: http://stackoverflow.com/q/41785005/878126 – android developer Jan 21 '17 at 22:01