Managing the Lifecycle of a Service--管理Service的生命周期

来源:互联网 发布:诚创cces电气设计软件 编辑:程序博客网 时间:2024/05/16 07:00

The lifecycle of a service is much simpler than that of an activity. However, it's even more important that you pay close attention to how your service is created and destroyed, because a service can run in the background without the user being aware.

(Service 的生命周期要比Activity的生命周期简单得多。然而,你要更加注意你的Service 是如何创建和如何销毁的,因为它可以在用户不知道的情况下一直运行下去。)

The service lifecycle—from when it's created to when it's destroyed—can follow two different paths:

(Service 的生命周期—从被创建到被销毁—有两种不同的方式:

1》A started service

The service is created when another component calls startService(). The service then runs indefinitely and must stop itself by calling stopSelf(). Another component can also stop the service by calling stopService(). When the service is stopped, the system destroys it..

1》被启动的Service 

当其他的组件调用startService()时,Service 被创建。然后它会无限期的运行下去,所以必须调用stopSelf()来终止它。其他的组件也可以通过调用stopService()来终止它。当Service 终止之后,系统会销毁它。

  )

2》A bound service

The service is created when another component (a client) calls bindService(). The client then communicates with the service through an IBinder interface. The client can close the connection by calling unbindService(). Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. (The service does not need to stop itself.)

2》当其他的组件(一个客户)调用bindService()时,Service 被创建。然后,客户可以通过一个IBinder接口和这个Service 交互。客户可以通过调用unBindService()来停止这个Service 。多个客户都可以绑定到同一个Service 上,当所有的客户通过unBindService解除绑定之后,系统会销毁它。(Service 本身不用再终止它自己了。)

These two paths are not entirely separate. That is, you can bind to a service that was already started with startService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by calling bindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.

这两种方式并不是完全分离的。也就是说,你可以绑定一个已经用startService()启动的Service 。例如,一个后台音乐服务可以通过一个标识要播放音乐的Intent,并调用startService()启动。之后,当用户想要对播放器进行一些操作,或者是获取一些当前歌曲的信息,可以使用一个Activity 调用bindService()绑定该Service。像这样的情况,直到所有已绑定的客户都解除绑定,stopService()或者stopSelf()才会真正的终止该Service。


Implementing the lifecycle callbacks

(实现Service 的生命周期函数)

Like an activity, a service has lifecycle callback methods that you can implement to monitor changes in the service's state and perform work at the appropriate times. The following skeleton service demonstrates each of the lifecycle methods:

跟Activity 类似,Service 也有生命周期函数,你可以通过实现它们用来监视Service 的状态并在适当的时候做一些操作。下面的Service 框架对Service 的每一个生命周期函数进行了说明:

import android.app.Service;import android.content.Intent;import android.os.IBinder;public class ExampleService extends Service {int mStartMode;// indicates how to behave if the service is killedIBinder mBinder; // interface for clients that bindboolean mAllowRebind; // indicates whether onRebind should be used@Overridepublic void onCreate() {// The service is being created// service 被创建}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// The service is starting, due to a call to startService()// 通过调用startService()方法,service 开始运行return mStartMode;}@Overridepublic IBinder onBind(Intent intent) {// A client is binding to the service with bindService()// 当一个客户通过bindService()方法绑定到一个servicereturn mBinder;}@Overridepublic boolean onUnbind(Intent intent) {// All clients have unbound with unbindService()// 当客户通过调用unbindService()解除绑定时return mAllowRebind;}@Overridepublic void onRebind(Intent intent) {// A client is binding to the service with bindService(),// after onUnbind() has already been called// 当onUnbind()方法已经被调用之后,// 客户通过bindService()方法再次绑定到该service时}@Overridepublic void onDestroy() {// The service is no longer used and is being destroyed// service 不会再被使用,将会被销毁}}

Note: Unlike the activity lifecycle callback methods, you are not required to call the superclass implementation of these callback methods.

注意:和Activity 的生命周期不太一样的是,你不必调用相应的父类生命周期方法。


Service的生命周期,左边的图表示通过调用startService()创建的Service 的生命周期,右边的图表示通过调用bindService()方法创建的Service的生命周期。


By implementing these methods, you can monitor two nested loops of the service's lifecycle:

(通过实现这些方法,你可以监视这两种Service 的生命状态)

1》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(), then stop the thread in onDestroy().

一个Service 的完整的生命周期发生在调用onCreate()方法和OnDestroy()方法返回之间。跟Activity 一样,Service 在onCreate()方法中完成它的初始化操作,并在onDestroy()方法中释放所占有的资源。例如,XXXX


The onCreate() and onDestroy() methods are called for all services, whether they're created by startService() or bindService().

无论Service 是通过startService()方法还是bindService()方法创建的,它们都会调用onCreate()方法和onDestroy()方法。


2》The active lifetime of a service begins with a call to either onStartCommand() or onBind(). Each method is handed the Intent that was passed to either startService() or bindService(), respectively.
If the service is started, the active lifetime ends the same time that the entire lifetime ends (the service is still active even after onStartCommand() returns). If the service is bound, the active lifetime ends when onUnbind() returns.

一个Service 的活跃生命周期时间从调用onStartCommand()方法或者调用onBind()方法开始。每一个方法都会分别接收startService()方法或者bindService()方法传递过来的Intent.

如果Service是通过startService()方法启动的,其活跃生命周期和整个的生命周期同时终止(即使onStartCommand()方法返回之后,该Service 仍处于活跃状态)。如果Service是通过bindService()方法启动的,当onUnbind()方法返回时,其活跃生命周期结束。

Note: Although a started service is stopped by a call to either stopSelf() or stopService(), there is not a respective callback for the service (there's no onStop() callback). So, unless the service is bound to a client, the system destroys it when the service is stopped—onDestroy() is the only callback received.

注意:尽管一个通过startService()方法启动的Service 可以通过调用stopSelf()方法或者stopService ()方法终止,但是Service 并没有相应的回调函数(并没有onStop()回调方法)。所以,除非Service 被绑定到一个client上,否则当它被终止时,系统就会销毁它,onDestroy()方法是唯一的回调方法。



Figure 2 illustrates the typical callback methods for a service. Although the figure separates services that are created by 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, a service that was initially started with onStartCommand() (by a client calling startService()) can still receive a call to onBind() (when a client calls bindService()).

插图2只是表示了一个Service的典型回调方法。尽管上图分开表示了通过startService()方法创建的service和通过bindService()方法创建的Service,但是,记住任何一个Service,不管它是如果被创建的,都允许clients绑定它。所以,一个最初用onStartCommand()方法初始化并启动的Service(client通过调用startService()方法)仍然可以接受onBind()调用(当一个client调用bindService()方法)



For more information about creating a service that provides binding, see the Bound Services document, which includes more information about the onRebind() callback method in the section about Managing the Lifecycle of a Bound Service.

更多关于创建提供binding 功能的service,请参考Bound Services 文档,在关于管理Bound Service生命周期部分包含了更多关于onRebind()回调方法的信息。

0 0
原创粉丝点击