Android Service系列(十五)管理service的生命周期

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简单。然而,你也要关注它的生死,因为它运行在后台。

 

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

翻译:service的生命周期有以下两条路径。

 

  • 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.

  • 翻译:从startService 到stopSelf或者stopService

  • 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.

  • 翻译:从bindService到 unBindService。 都解绑则service销毁。

 

These two paths aren't entirely separate. You can bind to a service that is already started with startService(). For example, you can start a background music service 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 such as this, stopService() or stopSelf() doesn't actually stop the service until all of the clients unbind.

翻译:两条路径有交叉。startService之后也能被绑定。

如果startService被绑定了,则 stopService 或者stopSelf 不能关闭service,这时候要等都解绑才行。

 

Implementing the lifecycle callbacks

实现生命周期方法

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:

如下:

public class ExampleService extends Service {
    int startMode;       // indicates how to behave if the service is killed
    IBinder binder;      // interface for clients that bind
    boolean allowRebind; // indicates whether onRebind should be used

    @Override
    public void onCreate() {
        // The service is being created
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // The service is starting, due to a call to startService()
        return mStartMode;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // A client is binding to the service with bindService()
        return mBinder;
    }
    @Override
    public boolean onUnbind(Intent intent) {
        // All clients have unbound with unbindService()
        return mAllowRebind;
    }
    @Override
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService(),
        // after onUnbind() has already been called
    }
    @Override
    public void onDestroy() {
        // The service is no longer used and is being destroyed
    }
}

 

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

注意:service的生命周期方法不需要call super

看下图

Figure 2. The service lifecycle. The diagram on the left shows the lifecycle when the service is created with startService() and the diagram on the right shows the lifecycle when the service is created with bindService().

翻译:左start  右bind

 

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. 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()).

翻译:startService也能被绑定。。。

 

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

翻译:实现这些方法,你就能监控这两个内置的生命周期循环

 

The entire lifetime of a service occurs between the time that onCreate() is called and the time that 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 can create the thread where the music is played in onCreate(), and then it can stop the thread in onDestroy().

翻译:从onCreate 到 onDestroy

 

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

注意:onCreate 和 onDestroy方法 不论是绑定还是start都会调用

 

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().

If the service is started, the active lifetime ends at 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.

翻译:活跃的生命周期起步于 onStartCommand 或者onBind。 每个方法都被传入通过 startService 或者bindService传入的方法。

如果service 是started 的,活跃的生命周期结束于整个生命周期结束。

如果service是bound的,则活跃的生命周期止步于 onUnbind返回。

 

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

注意:startService 被stop 的时候没有回调。 除非bind到一个client,否则,onDestroy是唯一的回调。。

 

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.

参考:Bound Services 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值