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 callingstopSelf()
. Another component can also stop the service by callingstopService()
. 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 anIBinder
interface. The client can close the connection by callingunbindService()
. 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。
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 timeonDestroy()
returns. Like an activity, a service does its initial setup inonCreate()
and releases all remaining resources inonDestroy()
. For example, a music playback service could create the thread where the music will be played inonCreate()
, then stop the thread inonDestroy()
.The
onCreate()
andonDestroy()
methods are called for all services, whether they're created bystartService()
orbindService()
. - The active lifetime of a service begins with a call to either
onStartCommand()
oronBind()
. Each method is handed theIntent
that was passed to eitherstartService()
orbindService()
, 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 whenonUnbind()
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.