android manifest.xml配置文件中的 Service节点

<service>

语法:
<service android:enabled=["true" | "false"]
         android:exported=["true" | "false"]
         android:icon="drawable resource"
         android:isolatedProcess=["true" | "false"]
         android:label="string resource"
         android:name="string"
         android:permission="string"
         android:process="string" >
    . . .
</service>
包含在:
<application>
能包含:
<intent-filter>
<meta-data>

简介:
声明一个service(Service的子类)作为应用程序的组件。 与activities不同,services没有用户界面。它们被用来实现了后台长期运行操作或者提供一个丰富的交互API供其它应用程序使用。所有的service在manifest文件中用<service>标签代表。android系统不能发现没有声明在manifest文件中的service,service将不能运行。


属性集:
android:enabled
指示这个service是否能被android系统实例化。true:能实例化,false:不能实例化。默认值:true。
<application> 标签也有 enabled 属性。这个属性应用到所有的应用程序组件,包括其中的services。 <application><service>的enabled属性必须都设置为 true时,service才是可被实例化的。任何一个的enabled属性被设置为fase,service不能被实例化。


android:exported
指示其他应用程序的组件是否可以调用service或者和service交互。true:可以,false:不可以。当值是false的时候,仅有本应用程序的组件或者有相同的USER ID的应用能启动service或者绑定service。默认值依赖于service包含的 intent filters。如果没有包含filters表示能够通过指定sevice详细的类名调用service。这意味着service倾向于仅仅是为应用程序内部使用,因为其他人不知道service的类名。因此在这种情况下,默认值就是false。另一方面,至少有一filter意味着service倾向于为外部使用,因此默认值为true。

这个属性不是唯一的方式限制对其他的应用程序公开service。你也能通过权限限制能与service交互的外部实例(具体看 permission属性)。


android:icon
代表service的图标。这个属性必须设置一个对drawable resource的引用。如果没有设置,用application的icon属性 值代替。
service的icon无论是在这儿设置还是用<application>中的icon,都是service的intent filter的默认icon (查看 the <intent-filter> element's icon attribute)。

android:isolatedProcess
如果设置为true,service将运行在一个特殊的进程中。这个进程是从系统的剩余进程中独立出来的,没有任何它的权限。只能通过service提供的API(bingding and starting) 与其进行交互。

android:label
A name for the service that can be displayed to users.If this attribute is not set, the label set for the application as a whole isused instead (see the <application> element's label attribute).

The service's label — whether set here or by the<application> element — is also thedefault label for all the service's intent filters (see the<intent-filter> element'slabel attribute).

The label should be set as a reference to a string resource, so thatit can be localized like other strings in the user interface.However, as a convenience while you're developing the application,it can also be set as a raw string.

android:name
The name of the Service subclass that implementsthe service. This should be a fully qualified class name (such as," com.example.project.RoomService"). However, as a shorthand, ifthe first character of the name is a period (for example, " .RoomService"),it is appended to the package name specified in the <manifest> element.

Once you publish your application, you should notchange this name (unless you've set android:exported="false").

There is no default. The name must be specified.

android:permission
The name of a permission that an entity must have in order tolaunch the service or bind to it. If a caller of startService(), bindService(), or stopService(),has not been granted this permission, the method will not work and theIntent object will not be delivered to the service.

If this attribute is not set, the permission set by the<application> element'spermissionattribute applies to the service. If neither attribute is set, the service isnot protected by a permission.

For more information on permissions, see thePermissionssection in the introduction and a separate document,Security and Permissions.

android:process
The name of the process where the service is to run. Normally,all components of an application run in the default process created for theapplication. It has the same name as the application package. The <application> element's processattribute can set a differentdefault for all components. But component can override the defaultwith its own process attribute, allowing you to spread yourapplication across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a newprocess, private to the application, is created when it's needed andthe service runs in that process.If the process name begins with a lowercase character, the service will runin a global process of that name, provided that it has permission to do so.This allows components in different applications to share a process, reducingresource usage.

Android Manifest 文件是一个重要的配置文件,它用于描述应用程序的组件、权限、应用程序级别的元数据等信息。在 Manifest 文件,我们可以定义各种组件,如 Activity、Service、BroadcastReceiver、ContentProvider 等等。 如果你想动态设置 AndroidManifest.xml Service属性,可以通过以下步骤实现: 1. 获取 AndroidManifest.xml 文件的绝对路径。 ```java String manifestPath = getApplicationContext().getPackageResourcePath() + "/AndroidManifest.xml"; ``` 2. 解析 AndroidManifest.xml 文件,获取 Service节点。 ```java DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new File(manifestPath)); doc.getDocumentElement().normalize(); NodeList serviceList = doc.getElementsByTagName("service"); ``` 3. 遍历 Service 节点,找到指定的 Service,并设置其属性。 ```java for (int i = 0; i < serviceList.getLength(); i++) { Node serviceNode = serviceList.item(i); NamedNodeMap serviceAttrMap = serviceNode.getAttributes(); String serviceName = serviceAttrMap.getNamedItem("android:name").getNodeValue(); if (serviceName.equals("com.example.MyService")) { // 设置 Service属性 serviceAttrMap.getNamedItem("android:exported").setNodeValue("true"); break; } } ``` 4. 保存修改后的 AndroidManifest.xml 文件。 ```java TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream(manifestPath)); transformer.transform(source, result); ``` 需要注意的是,上述代码需要在 AndroidManifest.xml 声明读写权限。在 Manifest 文件添加以下代码: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值