Syntax (syntax ):
<Serviceandroid: enabled = ["true"
| "False"]
Android: exported = ["true"
| "False"]
Android: icon ="Drawable
Resource"
Android: Label ="String
Resource"
Android: Name ="String"
Android: Permission ="String"
Android: Process ="String">
...
</Service>
Included in (contained in ):
<Application>
Possible Elements (can contain ):
<Intent-filter>
<Meta-data>
Description ):
This element is used to declare a service (subclass of the service class) as one of the components of the application. Unlike activity, the Service does not have a visible user interface. They are used to implement long-time background operations or provide communication APIs that can be called by other applications.
All services must be represented by the <service> element in the configuration file. Any service that is not declared in the list cannot be seen or run by the system.
Attributes (attributes ):
Android: Enabled
This attribute is used to indicate whether the service can be instantiated. If it is set to true, it can be instantiated; otherwise, it cannot be instantiated. The default value is true.
<Application> An element has its own enabled attribute, which applies to all components in the application, including service components. For a service that is enabled, the enabled attribute of the <Application> and <service> elements must be true (the default value is true ). If the enabled attribute of an element is set to false, the service is disabled and cannot be instantiated.
Android: exported
This attribute indicates whether the service can be called or interacted with by other application components. If it is set to true, it can be called or interacted. Otherwise, it cannot. If it is set to false, only components of the same application or applications with the same user ID can start or bind the service.
Its default value depends on the filter contained in the service. Without a filter, the service can only be called by specifying a specific class name. This means that the service can only be used within the application (because other external users do not know the class name of the Service ), therefore, the default value of this attribute is false. On the other hand, if at least one filter is included, it means that the service can provide services to other external applications, so the default value is true.
This attribute is not the only method that limits the exposure of services to other applications. You can also use permissions to restrict external entities that can interact with the service.
Android: icon
This attribute defines an icon that represents a service. It must reference a customizable resource that contains image definitions. If this attribute is not set, the icon set by the icon attribute of the <Application> element is used.
Whether the icons set by the <Application> element or the icons set by the <service> element, they are the default icons of the intent filter of the service.
Android: Label
This attribute is used to set the name of the service to be displayed to the user. If this attribute is not set, the label attribute value of the <Application> element is used instead.
Both the <service> label and the <Application> element label are the default tags of the intent filter of the service.
This tag application references a string resource so that it can be localized just like a string in the user interface. However, you can use native strings to set this attribute for ease of application development.
Android: Name
This attribute is used to specify the Class Name of the Service subclass that implements the service. It should be a complete Java class name (for example, Com. example. Project. roomservice ). However, you can also use abbreviations (for example,. roomservice). The system adds the value set for the package attribute in the <manifest> element to the beginning of the abbreviated name.
This name should not be changed once the application is published (unless Android: exported = "false ").
This property has no default value and must be specified.
Android: Permission
This attribute defines the permissions required for entities to start or bind services. If the caller's startservice (), bindservice (), and stopservice () methods are not granted this permission, these methods will not work and the intent object will not be sent to the service.
If this attribute is not set, the permission set through the permission attribute of the <appliction> element applies to this service. If no permission is set for the <Application> element, the service is not protected by the permission.
Android: Process
This attribute is used to set the name of the process running the service. Generally, all components of an application run in the process created for the application. The process name is the same as the package name of the application. <Application> the process attribute of the element can set a different default name for all components of the application. However, each component's own process attribute can overwrite this default value, which allows the application to be separated into multiple processes.
If the attribute value starts with ":", a new private process of the application will be created and the service will run in the process as needed. If the attribute value starts with a lower-case letter, the service will run in the global process named after this attribute value, and it provides the permission to make it work. In this way, different application components are allowed to share the process, thus reducing resource usage.
Introduced version (introduced in ):
API Level 1