Service官方教程(4)两种Service的生命周期函数

Managing the Lifecycle of a 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.

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

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

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

  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.

 started和bound类的service 生命周期函数不是完全独立的,started的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:

 1 public class ExampleService extends Service {
 2     int mStartMode;       // indicates how to behave if the service is killed
 3     IBinder mBinder;      // interface for clients that bind
 4     boolean mAllowRebind; // indicates whether onRebind should be used
 5 
 6     @Override
 7     public void onCreate() {
 8         // The service is being created
 9     }
10     @Override
11     public int onStartCommand(Intent intent, int flags, int startId) {
12         // The service is starting, due to a call to startService()
13         return mStartMode;
14     }
15     @Override
16     public IBinder onBind(Intent intent) {
17         // A client is binding to the service with bindService()
18         return mBinder;
19     }
20     @Override
21     public boolean onUnbind(Intent intent) {
22         // All clients have unbound with unbindService()
23         return mAllowRebind;
24     }
25     @Override
26     public void onRebind(Intent intent) {
27         // A client is binding to the service with bindService(),
28         // after onUnbind() has already been called
29     }
30     @Override
31     public void onDestroy() {
32         // The service is no longer used and is being destroyed
33     }
34 }

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

 service的生命周期函数,不是必需调用父类的,如super.xxx。

      

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

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

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

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

    -started的service一直存活,直到系统清理内存时,才有可能杀死它。
    -bound的service活到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.

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

  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.

 

转载于:https://www.cnblogs.com/sjjg/p/5898409.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值