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它。