Service lifecycle--服务组件的生命周期
A service can be used in two ways--翻译:服务组件有两种使用方式:
* It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started by calling Context.startService() and stopped by calling Context.stopService(). It can stop itself by calling Service.stopSelf() or Service.stopSelfResult(). Only one stopService() call is needed to stop the service, no matter how many times startService() was called.
翻译:第一种方式:服务被启动后一直将运行到被终止或是服务自行终止。这时候服务是被Context.startService()方法启动的,服务的终止是被Context.stopService()方法的执行被终止的,服务自行终止是通过执行Service.stopSelf() 或 Service.stopSelfResult()方法的。无论服务组件实例的startService()方法被执行了几次,只需执行 stopService()方法一次,服务就被终止。
* It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling Context.bindService(), and is closed by calling Context.unbindService(). Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.
翻译:另一种方式是:借助接口。客户端代码创建一个针对服务对象的连接,然后使用该连接来调用服务。创建连接的方法是:Context.bindService()方法;终止服务可以调用的方法是Context.unbindService()。多个客户程序可以绑定到同一个服务上,如果被绑定的服务还没有启动,可以使用bindService()方法来启动服务。
The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed.
翻译:这两种方式不是说完全独立的,我们可以绑定已经用startService()方法启动的服务,例如:可以执行startService()方法启动后台音乐服务,在该方法的参数intent中指定了要播放的音乐。稍后,用户可能在播放器上做些操作或是想看到正在播放的音乐的一些相关信息,那么可以有一个activity来创建与后台音乐服务的连接(执行bindService()方法),这个场合下,调用stopService()方法实际上是不能终止该服务的,除非最后一个绑定被解除。
Like an activity, a service has lifecycle methods that you can implement to monitor changes in its state. But they are fewer than the activity methods — only three — and they are public, not protected:
翻译:和 activity 组件类似, service组件实例也有生命周期方法,可以被实现,但是只有三个方法,这三个方法都是public,不是protected方法:
void onCreate()
void onStart(Intent intent)
void onDestroy()
By implementing these methods, you can monitor two nested loops of the service's lifecycle:--翻译:实现这三个方法可以监听服务组件实例生命周期中的两个状态切换循环过程:
* The entire lifetime of a service happens between the time onCreate() is called and the time onDestroy() returns. Like an activity, a service does its initial setup in onCreate(), and releases all remaining resources in onDestroy(). For example, a music playback service could create the thread where the music will be played in onCreate(), and then stop the thread in onDestroy().
翻译:整个生命期起于onCreate() 方法被调用,结束于 onDestroy()方法被执行完成。这两个方法作用和activity的同名方法类似,一个执行初始化操作,一个执行资源释放操作。例如:音乐回放服务可以在 onCreate()方法中创建一个线程来执行音乐播放操作,之后, onDestroy()方法中终止该线程。
* The active lifetime of a service begins with a call to onStart(). This method is handed the Intent object that was passed to startService(). The music service would open the Intent to discover which music to play, and begin the playback.
翻译: 激活期起于onStart()方法被执行,该方法根据startService()方法的参数intent,做onStart相关的操作(音乐服务根据intent参数来查找要播放的音乐),然后执行回放。
There's no equivalent callback for when the service stops — no onStop() method.
翻译:要注意的是,服务终止的时候并没有对应的onStop()回调方法。
The onCreate() and onDestroy() methods are called for all services, whether they're started by Context.startService() or Context.bindService(). However, onStart() is called only for services started by startService().
翻译:无论服务组件实例是通过 Context.startService() 方法还是通过 Context.bindService()方法被启动的,系统都会调用到服务实例的 onCreate() 和 onDestroy() 方法。但是onStart()方法的调用只能针对由startService()启动的服务。
If a service permits others to bind to it, there are additional callback methods for it to implement:---翻译:如果某个服务允许被绑定,那么这个服务组件还有其他回调方法可以被实现:
IBinder onBind(Intent intent)
boolean onUnbind(Intent intent)
void onRebind(Intent intent)
The onBind() callback is passed the Intent object that was passed to bindService and onUnbind() is handed the intent that was passed to unbindService(). If the service permits the binding, onBind() returns the communications channel that clients use to interact with the service. The onUnbind() method can ask for onRebind() to be called if a new client connects to the service.
翻译:onBind() 回调方法的参数是一个intent对象,该对象实际上是通过 Context.bindService 方法传入的, onUnbind()方法的参数是Context.unbindService()方法传入的,如果服务允许绑定,那么,onBind() 返回的是客户端需要与该服务实例交互的通讯通道对象;当有新的客户需要和该服务连接的时候,可以通过 onUnbind() 方法来执行 onRebind() 方法,以完成重新绑定的操作。
The following diagram illustrates the callback methods for a service. Although, it separates services that are created via startService from those created by bindService(), keep in mind that any service, no matter how it's started, can potentially allow clients to bind to it, so any service may receive onBind() and onUnbind() calls.
翻译:下面用两个图说明了服务的回调方法,虽然这里我们是分别基于两种启动服务的方式(Context.startService()、Context.bindService())来做说明的,但是要时刻记住:服务组件往往都是允许被绑定,所以即使一个服务组件实例是被Context.startService方法启动的,这个服务组件实例实际上是有onBind() 、 onUnbind()回调方法。