Android Development Tour (ii) service life cycle and broadcast receiver life cycle

Source: Internet
Author: User

Introduction

Application components have a life cycle--the first time Android instantiates their response intent until the end of the instance is destroyed. During this time, they are sometimes active, sometimes inactive, and sometimes visible to the user for the activity, sometimes invisible. The component life cycle discusses the life cycle of activities, services, and broadcast receivers-including their possible state during the life cycle, methods for notifying state changes, and the possibility of the process of hosting the components of those States being terminated and instances being destroyed.

Previous Android Development Tour: Component life cycle (i) describes the life cycle of activities and their possible state, notification state change methods. This article describes the life cycle of service and broadcast receivers:

Service life cycle
Broadcast receiver life cycle

1. Service life cycle

A service can be used in two ways:

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 stopped by calling Context.stopservice (). The service can also stop itself by calling Service.stopself () or Service.stopselfresult (). You only need to call StopService () to stop the service, regardless of how many times you call StartService ().
Services can be manipulated programmatically by using related interfaces. The client establishes a connection to the service object and uses the connection to dial in the service. The connection is established by calling Context.bindservice () and is 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.

These two patterns are not completely separate. You can bind to a service that starts with StartService (). For example, a background music service can start by calling StartService () by using the intent object that defines the music playback. Until later, the user may want to do some control on the player or get some information about the current song, an activity will call Bindservice () to establish a connection with the service. In this case, the StopService () is actually not stopped until the last binding is closed.

Like activity, a service also has a lifecycle method that you can perform to monitor its state changes. However, there are fewer life-cycle methods than activities, only three and they are publicly owned (public) rather than protected (protected) (Description: The active life cycle method is protected):

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

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

The entire life time of the service (entire lifetime), from calling OnCreate () to calling Ondestory () accordingly. Like an activity, the service does some initial setup 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 ().
The active life time of the service (active lifetime), starting with the call to OnStart (). This method passes parameters to the intent object that is passed to StartService (). The music service will open intent, learn which music to play and start playing.
There is no corresponding callback method because the service stops without the OnStop () method.

StartService () and ondestory () are invoked by all services, whether initiated through Context.startservice () or through Context.bindservice (). However, OnStart () is only called by a service that is started by StartService ().

If a service allows other bindings to it, there are some additional callback methods to implement it:

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

The arguments passed by the Onbind () callback are intent objects passed to Bindservice (), and the arguments passed by the Onunbind () callback are Unbindservice objects 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 connects to the service.

Explains the callback method for the service. Although it separates services started by StartService () and services started by Bindservice (), remembering any service, regardless of how it was started, could allow the client to bind to it, so any service may receive onbind () and Onunbind () calls.

Service_lifecycle
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. The broadcast receiver is considered 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. However, the system can kill only inactive components at any time when the process is needed by other processes that 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 () derived thread then returns, the entire process, including the new thread, is judged inactive (unless the other application component in the process is active), it will put it in a crisis of being killed. The way to solve this problem is to OnReceive () Start a service and do the work in a timely manner, so the system knows that there is active work in progress.



Other articles in this series:

Android Development Tour: Environment Building and HelloWorld
Android Development Tour: The directory structure of the HelloWorld project
Android Development Tour: Android architecture
Android Development Journey: Application Basics and Components
Android Development Journey: Application Basics and Components (cont.)
Android Development Tour: Events and Tasks
Android Development Tour: Processes and Threads
Android Development Tour: Component life cycle (i)
It is recommended that you look again at the beginning, there will definitely be a new harvest!
From the Android architecture to this basic content is free translation from the Android SDK documentation.

Original address: http://www.cnblogs.com/skynet/archive/2010/05/06/1729332.html
Wu Qin
Source: http://www.cnblogs.com/skynet/
This article is based on the attribution 2.5 China mainland license agreement, which is reproduced, interpreted or used for commercial purposes, but must retain the attribution Wu Qin (including links) herein.

Android Development Tour (ii) service life cycle and broadcast receiver life cycle

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.