Android Service Lifecycle Detailed _android

Source: Internet
Author: User
Tags call back

Introduction

The application component has a lifecycle--the beginning of the Android instantiation they respond to the intent until the end instance is destroyed. During this time, they are sometimes active, sometimes inactive, and sometimes visible to the user and sometimes invisible to the activity. The component lifecycle will discuss the lifecycle of activities, services, and broadcast receivers-including their possible state in the lifecycle, the methods of notifying state changes, and the possibility that the components of these states are hosted by the process that is terminated and instances are destroyed.

On the Android Development tour: Component Lifecycle (i) explains the life cycle of activities and their possible states, and the ways in which notification status changes. This article describes the lifecycle of service and broadcast receivers:

Service life cycle

Broadcast receiver life cycle

1. Service life cycle

A service can be used in two areas:

It can be started and allowed to run until someone stops it, or it stops itself. In this mode, the service is started by calling Context.startservice () and stopping the service by calling Context.stopservice (). The service can also stop itself by calling Service.stopself () or Service.stopselfresult (). Only one stopservice () stop service is required, regardless of how many times the call StartService () is invoked.

Services can be manipulated programmatically by using the relevant interfaces. The client establishes a connection with the service object and uses the connection to transfer the service. The connection is established by calling Context.bindservice () and closed by calling Context.unbindservice (). Multiple clients can bind to the same service. If the service has not started, Bindservice () can choose to start it.

The two models are not completely separate. You can bind to a service that starts with a startservice (). For example, a background music service can be started by invoking StartService () by using a intent object that defines music playback. Until later, the user might want to control the player or get some information about the current song, and an activity would invoke Bindservice () to establish a connection to the service. In this case, actually until the last binding closes StopService () does not stop.

Like an activity, a service has a lifecycle approach, and you can perform state changes that monitor it. However, there are fewer than the active lifecycle methods, only three and they are public rather than protected (protected) (note: The life cycle method of the activity is protected):

void OnCreate ()
void OnStart (Intent Intent)
void Ondestory ()

With these three methods, you can monitor the two nested loops of the service lifecycle:

The entire life time of the service (entire lifetime), from the call OnCreate () to the corresponding call to Ondestory (). Like an activity, the service makes some initial settings in OnCreate () and frees all resources in. For example, a music playback service can create a thread in OnCreate () and then stop the thread in Ondestory ().

Active lifetime of the service, starting with the call to OnStart (). This method passes the argument to the intent object that is passed to StartService (). The music service will open intent to learn which music to play and start playing.

There is no corresponding callback method because the service stopped without the OnStop () method.

StartService () and ondestory () are invoked by all services, either through Context.startservice () or through Context.bindservice (). However, OnStart () is only invoked by a service initiated by StartService ().
If a service allows something else to bind to it, there are some additional callback methods to implement it:

IBinder Onbind (Intent Intent)
Boolean onunbind (Intent Intent)
void Onrebind (Intent Intent)

The parameter passed by the Onbind () callback is the intent object passed to Bindservice (), and the parameter passed by the Onunbind () callback is the Unbindservice object passed to intent (). If the service allows binding, Onbind () returns the communication channel that the client interacts with the service. The Onunbind () method can require a call to Onrebind () if a new client is connected to the service.

The following figure explains the callback method for the service. Although it separates the service initiated by StartService () and the service initiated by Bindservice (), remember that any service, regardless of how it starts, may allow the client to bind to it, so any service may receive onbind () and Onunbind () calls.

You can find that the first time StartService, will call OnCreate and OnStart, no stopservice before, no matter how many times you click StartService, will only call OnStart. Call OnDestroy when StopService. Clicking StopService again will find that it will not enter the life cycle of the service, that is, Oncreate,onstart and OnDestroy will not be called again.

And Onbind is not invoked in Startservice/stopservice.

Note that when the activity exits, the Sercvice does not stop, and then we can again enter the activity rebind, when the service calls the Onrebind () method, but the call to Onrebind () The premise of the method is that the previous Onunbind () method succeeded, but the use of super.onunbind (intent) was unsuccessful, and we had to manually make it return true, and then bind Rebind () to execute it again. Otherwise, if the specified Onunbind () that is not displayed when exiting is successful (false), the Onbind () method and onrebind of the service are not executed when the activity is restarted to bind the services. But the Serviceconnection method will surely be recalled. This means that the Onbind () method in the service is different from the OnStart () method and cannot be called repeatedly.

2. Broadcast receiver life cycle

A broadcast receiver has a callback method: void OnReceive (Context Curcontext, Intent broadcastmsg). When a broadcast message arrives at the receiver, Android calls its OnReceive () method and passes it to the intent object that contains the message. Broadcast receivers are thought to be active only when it executes this method. When OnReceive () returns, it is inactive.

The process of having an active broadcast receiver is protected and will not be killed. But the system can kill processes that have only inactive components at any time, while other processes need to occupy the memory.

This poses a problem when the response of a broadcast message is time-consuming, so it should be done in a separate thread, away from the main thread that the other components of the user interface are running. If the onreceive () derivative thread is then returned, the entire process, including the new thread, is judged inactive (unless the other application components in the process are active), which will put it in the midst of a kill crisis. The solution to this problem is to OnReceive () Start a service to do this work in a timely manner, so the system knows that there is active work in progress.

Ps:service Class Two startup methods:

Context.startservice ()
Context.bindservice ()

1. Invoking the StartService () method anywhere in the same application can start the service, and then the OnCreate () and OnStart () methods of the service class are recalled. This startup Service runs in the background until the Context.stopservice () or Selfstop () method is invoked.
In addition, if a Service has been started, other code attempts to invoke the StartService () method will not execute onCreate (), but will perform a onStart () again.

2. Another bindservice () method means that the service and the customer class that invoked the service are tied up and the service is destroyed if the call to the client class is destroyed. One advantage of this approach is that the Bindservice () method executes after the service recalls the Onbind () side of the above mentioned, from which you can return a class that implements the Ibind interface, which can communicate with the service on the client side, such as getting service The state or other operation that is running. If the service is not yet running, starting the service using this method will OnCreate () the method without calling OnStart ().

Summarize:

1. The purpose of the StartService () is to callback the OnStart () method, which is invoked when the service does not exist, if the service exists (for example, before the Bindservice is invoked, Then the OnCreate method of the service has been invoked) so StartService () will skip the OnCreate () method.

2. The purpose of the Bindservice () is to call back the Onbind () method, which is to establish a bridge between the service and the caller and not be responsible for more work (for example, a service needs to connect to the server), Typically, Bindservice is used to bind to an existing service (that is, services started through StartService).

Because the service's OnStart () method is only invoked when StartService () starts the service, use OnStart () to pay attention to this.

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.