Android Service系列(六)在清单文件申明service及其属性要点

You must declare all services in your application's manifest file, just as you do for activities and other components.

翻译:使用service必须要在清单文件申明

To declare your service, add a <service> element as a child of the <application> element. Here is an example:

如下:

<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService" />
      ...
  </application>
</manifest>

See the <service> element reference for more information about declaring your service in the manifest.

翻译:参考: <service>

There are other attributes that you can include in the <service> element to define properties such as the permissions that are required to start the service and the process in which the service should run.

翻译:在<service>元素中还能加permission等等属性

The android:name attribute is the only required attribute—it specifies the class name of the service. 

翻译: android:name 属性是唯一必须的

After you publish your application, leave this name unchanged to avoid the risk of breaking code due to dependence on explicit intents to start or bind the service (read the blog post, Things That Cannot Change).

翻译:发布app后,service名字就不要变了,因为有可能显示调用

Caution: To ensure that your app is secure, always use an explicit intent when starting a Service and don't declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you cannot be certain of the service that responds to the intent, and the user cannot see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent.

翻译:andorid5.0后,bindService不支持隐式调用了。

 

You can ensure that your service is available to only your app by including the android:exported attribute and setting it to false. This effectively stops other apps from starting your service, even when using an explicit intent.

翻译:android:exported属性能用来决定是否给其他app使用这个service

 

Note: Users can see what services are running on their device. If they see a service that they don't recognize or trust, they can stop the service. In order to avoid having your service stopped accidentally by users, you need to add theandroid:description attribute to the <service> element in your app manifest. In the description, provide a short sentence explaining what the service does and what benefits it provides.

翻译:使用 android:description描述这个servie,这样用户就能看到这个“sentence”,这样可能就不会手动kill它。

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值