What is the purpose of disabling Android service in manifest?

In Android, you can create a service to perform background tasks, etc., by subclassing the service. He orders to use the Service, which must be specified in the manifest for the application:

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

One of the service parameters in the manifest is the "enabled" option:

Whether or not the system can be created by the system is "true" if it can be, and "false" if not.

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

The only thing I can see for a service to be disabled in the manifest, and seems to be of limited value, is if it is only used for debugging and I want it to be disabled for production. Did I miss something?

+3


source to share


2 answers


The attribute is android:enabled

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

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

.

Besides including this attribute in your manifest, you need to do the following:

In the resource file bool.xml

under the section res/values/

add the following line:



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

      

In the resource file bool.xml

under the section res/values-v19/

add this line:

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

      

+4


source


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

In the most specific case of a Service

, I agree that it would be rare for you to disable it. One possibility could be that a service that connects to the system (e.g. input method editor, accessibility service) that you only want to enable at runtime (via PackageManager

and setComponentEnabledSetting()

), if the user makes an in-app purchase, opens this function ... I'm sure there are other scenarios for this Service

, although at this early hour of the day nobody catches your eye (yawn!).



However, I suspect that it Service

"inherits" its parameter android:enabled

because of one of the Android component types, as well as actions, providers and receivers. Other scripts android:enabled

will be more common with other types of components. For example, it is considered good form to keep your receiver BOOT_COMPLETED

unplugged until you know you need it. So, for example, if a receiver BOOT_COMPLETED

is only used to resume a download interrupted by a reboot, you only need that receiver if you are downloading. You can also disable it at any other time, so you don't waste user time on "regular" reboots.

+1


source







All Articles