Life cycle of Service

Source: Internet
Author: User

managing the Lifecycle of a Service

The life cycle of a service, from the time it is created, to the time it is destroyed, can have two different paths:

A started service

The service that is opened is created by calling StartService () from another component.

This service can run indefinitely, and you must call the stopself () method or other component to call the StopService () method to stop it.

When the service is stopped, the system destroys it.

A bound Service

The service being bound is created when another component (a customer) calls Bindservice () .

Customers can communicate via a IBinder interface and service.

Customers can use the unbindservice () method to close this connection.

A service can be bound to multiple customers at the same time, and the service is destroyed when multiple customers are unbound.

  These two paths are not completely separate.

That is, you can bind to a service that has been invoked with StartService () and is turned on.

For example, a background music service might be turned on by calling the StartService () method, and later, maybe the user wants to control the player or get some information about the current song, which can be bindservice ( ) binds an activity and service. In this case,stopservice () or stopself () will not actually stop the service unless all the customers are unbound.

Implementing the lifecycle callbacks

Like activity, the service has a range of lifecycle callback functions that you can implement to monitor changes in service state and perform appropriate work at the right time.

The following service shows the method for each life cycle:

PublicClass ExampleserviceExtendsservice{int Mstartmode;//Indicates how to behave if the service is killed IBinder Mbinder;//Interface for clients that bindBoolean mallowrebind;//Indicates whether Onrebind should be used@OverridePublicvoidOnCreate () {//The service is being created} @OverridePublicint Onstartcommand (Intent Intent,int flags,IntStartid) {//The service is starting, due to a call to StartService ()ReturnMstartmode; } @OverridePublicIBinder Onbind (Intent Intent) {//A client is binding to the service with Bindservice ()ReturnMbinder; } @OverridePublicBooleanOnunbind (Intent Intent) {// All clients has unbound with unbindservice () return Mallowrebind;} @Override public void Onrebind (Intent Intent) {// a client is binding to the service with Bindservice (), // a fter Onunbind () has already been Called @Override public void OnDestroy () {//< Span style= "color: #008000;" > The service is no longer used and is being Destroyed}}     

  Unlike the activity lifecycle callback function, you do not need to call the implementation of the base class.

This diagram illustrates the service's typical callback method, although this diagram separates the service that is being opened from the service of the binding, but you need to remember that any service potentially allows binding.

Therefore, a service that is turned on may still be bound.

By implementing these methods, you can see the life cycle of the two tiers of nested service:

The entire lifetime

The lifetime of the service is started from onCreate () and is called until the OnDestroy () method returns.

Like activity, the service initializes its work in onCreate () , releasing the remaining resources in OnDestroy () .

For example, a music playback service can create a music-playing thread in OnCreate () and stop the thread in Ondestory ().

onCreate () and OnDestroy () are called by all service, regardless of whether the service is through startservice () or Bindservice () established.

The active lifetime

The life time of the service active activity (active lifetime) is initiated from Onstartcommand () or onbind () , They each handle the Intent object passed by the StartService () or Bindservice () method.

If the service is turned on, its activity life cycle and the entire life cycle end together.

If the service is bound, its activity life cycle is ended after the Onunbind () method returns.

Note: Although an open service is stopped by calling Stopself () or stopservice () , there is no corresponding callback function corresponding to it, i.e. no onstop () callback method. So, when the Stop method is called, unless the service and the client component are bound, the system will destroy it directly, and the Ondestory () method will be called and the only callback method that will be called at this time.

managing the Lifecycle of a Bound Service

When the binding service and all clients unbind, the Android system will destroy it (unless it is also turned on by the Onstartcommand () method).

Therefore, if your service is a purely binding service, then you do not need to manage its life cycle.

However, if you choose to implement the Onstartcommand () callback method, you must explicitly stop the service because the service is now considered to be open.

In this case, the service will run until it calls itself stopSelf() or another component call stopService() , regardless of whether it is bound to the client or not.

Also, if your service is turned on and accepted for binding, then when the system calls your onUnbind() method, if you want to accept a onrebind ( instead of a call) at the next client binding onBind() , you can choose onUnbind() to Returns true in the.

  The return value of onrebind () is void, but the client still gets the object in its onServiceConnected() callback method IBinder .

Shows the life cycle of this service (which is enabled and also allowed to bind):

start service with Context.startservice ()

Its life cycle is Context.startservice ()->oncreate ()->onstart ()->service running-->context.stopservice () | ->ondestroy ()->service Stop if the service is not already running, Android calls OnCreate () and then calls OnStart (), and if the service is running, only OnStart () is called, So a service's OnStart method may be called repeatedly.
StopService Direct OnDestroy, if the caller is directly exiting without calling StopService, the service will always run in the background. The service can be shut down by StopService after the caller has started up again.
So the life cycle of calling StartService is: onCreate---OnStart (can be called multiple times)--OnDestroy
for Bindservice () Start Service experience: context.bindservice ()->oncreate ()->onbind ()->service running-- >onunbind (), OnDestroy ()->service stop
Onbind will return a Ibind interface instance to the client, Ibind allow the client to callback the service's methods, such as getting service running status or other operations. This time the caller (context, for example, activity) is bound together with the service, the context exits, Srevice will call Onunbind->ondestroy exit accordingly.
So the lifetime of the call Bindservice is: onCreate---Onbind (one time, not multiple bindings)--onunbind--and ondestory. Once the activity is destroyed it ends, and if you press home to put it backstage, then he doesn't quit.

add: only onstart can be called multiple times (via multiple StartService calls) during service each turn-off process, other oncreate,onbind,onunbind, Ondestory can only be called once in a life cycle.

Life cycle of Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.