Android Service life cycle

Source: Internet
Author: User

Life cycle of Android service

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:

public class Exampleservice extends service{int Mstartmode,//indicates how to behave if the Service is killed IBi NDEr Mbinder; Interface for clients this bind Boolean mallowrebind; Indicates whether Onrebind should be used @Override public void onCreate () {//The service is being CR eated} @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 are binding to the service with Bindservice (    ) return mbinder;        } @Override public boolean onunbind (Intent Intent) {//All clients has unbound with Unbindservice ()    return mallowrebind; } @Override public void Onrebind (Intent Intent) {//A client was binding to the service with Bindservice () ,//After Onunbind () had already been called} @OverriDe public void OnDestroy () {//The service was 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 () will be called by all service, regardless of whether the service is through StartService () Or is Bindservice () established.

The active lifetime

The life time of the service active activity (active lifetime) is invoked starting with Onstartcommand () or onbind () , and their respective justification The Intent object passed over 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):

Resources

API guides:services

Http://developer.android.com/guide/components/services.html

API Guides:bound Services

Http://developer.android.com/guide/components/bound-services.html

Android Service life cycle

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.