Life cycle of Android service

Source: Internet
Author: User

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

Start 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.
  
Bind 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 may be turned on by calling the StartService () method, and later, perhaps the user wants to control the player or get some information about the current song, and can bind an activity and service through Bindservice (). 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.

 Public  class exampleservice extends Service{    intMstartmode;//indicates how to behave if the service is killedIBinder Mbinder;//interface for clients that bind    BooleanMallowrebind;//Indicates whether Onrebind should be used    @Override     Public void onCreate()    {//The service is being created}@Override     Public int Onstartcommand(Intent Intent,intFlagsintStartid) {//The service is starting, due to a call to StartService ()        returnMstartmode; }@Override     PublicIBinderOnbind(Intent Intent) {//A client is binding to the service with Bindservice ()        returnMbinder; }@Override     Public Boolean Onunbind(Intent Intent) {//All clients has unbound with unbindservice ()        returnMallowrebind; }@Override     Public void Onrebind(Intent Intent) {//A client is binding to the service with Bindservice (),        //After Onunbind () have already been called}@Override     Public void OnDestroy()    {//The service is no longer used and is being destroyed}}

Unlike the life cycle callback function of an activity, 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 all service calls, regardless of whether the service is established through StartService () or Bindservice ().

The active lifetime
The life time of the service active activity (active lifetime) is initiated from Onstartcommand () or onbind () and they are processed by StartService () or Bindservice () The intent object that the method passes over.

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, that is, there is 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 runs until it calls Stopself () or another component calls StopService (), regardless of whether it is bound to the client or not.

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

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

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Life cycle of Android service

Related Article

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.