Detailed explanation of Android service life cycle

introduction

Application components have a life cycle -- at the beginning Android instantiates them to respond to the intent until the end of the instance is destroyed. During this period, they are sometimes active and sometimes inactive; Activities are sometimes visible to users and sometimes invisible. The component life cycle will discuss the life cycle of activities, services and broadcast recipients - including their possible states in the life cycle, the method of notifying state changes, and the possibility that the processes hosted by the components in these States will be terminated and instances will be destroyed.

Part I: Android development journey: component life cycle (I) explains the life cycle of activities, their possible states, and the method of notifying state changes. This chapter will introduce the service and broadcast receiver life cycle:

Service life cycle

Broadcast receiver lifecycle

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 (). A service can also stop itself by calling service. Stopself() or service. Stopselfresult(). You only need to call stopservice () once to stop the service, regardless of how many times startservice () is called.

Services can be programmatically operated by using related interfaces. The client establishes a connection with the service object and uses the connection to call in the service. The connection is established by calling context. Bindservice() and closed by calling context. Unbindservice(). Multiple clients can be bound to the same service. If the service has not been started, bindservice () can choose to start it.

The two models are not completely separated. You can bind to a service started with startservice (). For example, a background music service can be started by calling startservice () using an intent object that defines music playback. Until later, the user may want to control 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, stopservice () does not actually stop until the last binding closes.

Like activities, a service has a lifecycle approach that you can perform to monitor its state changes. However, there are fewer life cycle methods than activities, only three, and they are public rather than protected (Note: the life cycle methods of activities are protected):

void onCreate() void onStart(Intent intent) void onDestory()

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

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

The active lifetime of the service starts with the call to onstart(). The parameter passed by this method is the intent object passed to startservice (). The music service will open intent to find out which music to play and start playing.

There is no corresponding callback method because there is no onstop() method for service stop.

Startservice() and ondestory() are called by all services, whether they are started through context. Startservice() or context. Bindservice(). However, onstart() is only called by services started through startservice(). If a service allows others 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 parameters passed by the onbind() callback are the intent object passed to bindservice(), and the parameters passed by the onunbind() callback are the intent object passed to unbindservice(). If the service allows binding, onbind () returns the communication channel between the client and the service. The onunbind () method can require that onrebind () be called if a new client connects to the service.

The following figure illustrates the callback method of the service. Although it separates the service started by startservice () from the service started by bindservice (), remember that any service, no matter how it is started, may allow the client to bind to it, so any service may receive onbind () and onunbind () calls.

It can be found that oncreate and OnStart will be called the first time startservice is started. Before stopservice, only OnStart will be called no matter how many times startservice is clicked. Ondestroy is called when stopservice. Click stopservice again and you will find that it will not enter the service life cycle, that is, oncreate, OnStart and ondestroy will not be called again.

Onbind is not called in startservice / stopservice.

It should be noted that Sercvice will not stop when the activity exits. At this time, we can enter the activity for rebinding. At this time, the service will call onbind() method, but the premise of calling onbind() method is that the previous onunbind() method is executed successfully, but using super.onunbind (intent) is unsuccessful, At this time, we need to manually return it to true. When Binding again, bind () will execute. Otherwise, if the specified onunbind() not displayed during exit is successful (false), when this activity is restarted to bind the service, the onbind() method and onrebind method of the service will not be executed, but the serviceconnection method will certainly call back. This shows that the onbind () method in the service is different from the OnStart () method and cannot be called repeatedly.

2. Broadcast receiver lifecycle

A broadcast receiver has a callback method: void onReceive (context curcontext, intent broadcastmsg). When a broadcast message reaches the receiver, Android calls its onReceive () method and passes it to the intent object containing the message. The broadcast receiver is considered active only when it executes this method. When onReceive () returns, it is inactive.

A process with an active broadcast receiver is protected from being killed. However, the system can kill processes with only inactive components at any time, when the memory occupied by other processes needs it.

This brings a problem. When a broadcast message is responded, it takes time, so you should do these things in a separate thread, away from the main thread running other components of the user interface. If onreceive() spawns the thread and then returns, the whole process, including the new thread, is determined to be inactive (unless other application components in the process are active), which will put it in the danger of being killed. The method to solve this problem is onreceive() to start a service to do this work in time, so the system knows that there is active work in the process.

PS: service class has two startup methods:

• Context.startService() • Context.bindService()

1. Call the startservice () method anywhere in the same application to start the service, and then the system will call back the oncreate () and OnStart () methods of the service class. The service started in this way will run in the background until the context. Stopservice () or selfstop () method is called. In addition, if a service has been started and other codes try to call the startservice () method again, oncreate () will not be executed, but OnStart () will be executed again.

2. Another bindservice () method means to bind the service to the client class calling the service. If the client class calling the service is destroyed, the service will also be destroyed. One advantage of using this method is that after the bindservice () method is executed, the service will call back to the onbind () party mentioned above. You can return a class that implements the ibind interface from here. By operating this class on the client, you can communicate with the service, such as getting the running state of the service or other operations. If the service is not running yet, using this method to start the service will oncreate() method instead of calling onstart().

Summary:

The purpose of 1. startService () is to callback the onStart () method. The onCreate () method is called when Service does not exist. If Service exists (for example, before bindService is invoked, then Service's onCreate method has been invoked) then startService () will skip the onCreate () method.

2. Bindservice() is used to call back onbind() method. Its function is to build a bridge between the service and the caller. It is not responsible for more work (for example, a service needs to connect to the server). Bindservice is generally used to bind to an existing service (i.e. a service started through startservice).

Since the OnStart () method of the service is called only when startservice () starts the service, you should pay attention to this when using OnStart ().

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>