其他分享
首页 > 其他分享> > Android Service系列(七)startService简述

Android Service系列(七)startService简述

作者:互联网

A started service is one that another component starts by calling startService(), which results in a call to the service's onStartCommand() 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(), or another component can stop it by calling stopService().

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

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

 

An application component such as an activity can start the service by calling startService() and passing an Intentthat specifies the service and includes any data for the service to use. The service receives this Intent in the onStartCommand() 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(). The service receives the intent in onStartCommand(), 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

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

This is a subclass of Service 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(), 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.

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

 

标签:Service,service,start,started,component,application,activity,startService,Androi
来源: https://blog.csdn.net/perfectnihil/article/details/90442060