Android Service系列(七)startService简述
阅读原文时间:2021年04月20日阅读:1

A started service is one that another component starts by calling [startService()](https://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29), which results in a call to the service's [onStartCommand()](https://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29) method.

翻译:其他组件 调用 startService--->service调用 onStartCommand

When a service is started, it has a lifecycle that's independent of the component that started it. The service can run in the background indefinitely, even if the component that started it is destroyed. As such, the service should stop itself when its job is complete by calling [stopSelf()](https://developer.android.com/reference/android/app/Service.html#stopSelf%28%29), or another component can stop it by calling [stopService()](https://developer.android.com/reference/android/content/Context.html#stopService%28android.content.Intent%29).

翻译:service一旦start起来了,就和start他的那个组件没关系了。(启动它的那个组件关了也没关系)

因此,service要通过调用stopSelf或者其他组件调用stopService  来终结

An application component such as an activity can start the service by calling [startService()](https://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29) and passing an [Intent](https://developer.android.com/reference/android/content/Intent.html)that specifies the service and includes any data for the service to use. The service receives this [Intent](https://developer.android.com/reference/android/content/Intent.html) in the [onStartCommand()](https://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29) method.

翻译:应用组件能通过intent给service传递数据,service在onStartCommand中接收数据

For instance, suppose an activity needs to save some data to an online database. The activity can start a companion service and deliver it the data to save by passing an intent to [startService()](https://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29). The service receives the intent in [onStartCommand()](https://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29), connects to the Internet, and performs the database transaction. When the transaction is complete, the service stops itself and is destroyed.

例如:加入一个activity想存点数据到线上数据库,就可以balabala这样做,做完后stop itself就好

Caution: A service runs in the same process as the application in which it is declared and in the main thread of that application by default. If your service performs intensive or blocking operations while the user interacts with an activity from the same application, the service slows down activity performance. To avoid impacting application performance, start a new thread inside the service.

service默认在启动它的那个线程,这个说了好多次了。。。。。

Traditionally, there are two classes you can extend to create a started service:

有两个类 可以用来 started servcie

[Service](https://developer.android.com/reference/android/app/Service.html)

This is the base class for all services. When you extend this class, it's important to create a new thread in which the service can complete all of its work; the service uses your application's main thread by default, which can slow the performance of any activity that your application is running.

翻译:这个记得要自己起线程

[IntentService](https://developer.android.com/reference/android/app/IntentService.html)

This is a subclass of [Service](https://developer.android.com/reference/android/app/Service.html) that uses a worker thread to handle all of the start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. Implement [onHandleIntent()](https://developer.android.com/reference/android/app/IntentService.html#onHandleIntent%28android.content.Intent%29), which receives the intent for each start request so that you can complete the background work.

翻译:如果不需要同时处理多个请求的话,用这个类就行。 

实现onHandleIntent方法,这个方法会接收intent

The following sections describe how you can implement your service using either one for these classes.

翻译:下面描述怎么用这两个类