Android learning (2) service use and personal understanding


As we all know, Android has four major components: service, activity, content provider and broadcast receiver.

Activity should be the first and most contacted component. Speaking of it, I haven’t used the radio yet. The other three have more or less touched it.

Today, I’ll focus on service. After reading this section, I feel it’s very simple, that is, it’s good to understand some concepts.

Post the address of the official service document:

Service is a background service. When I first saw this, I thought it was similar to thread. In fact, the two are exactly the same. In fact, the service is still in the main thread. If time-consuming operations are added to the service, it will still cause ANR. His correct understanding should be the operation called by the application in the background, because there is no need to render the UI at this time.

You can create a thread in the service to perform time-consuming tasks. In that case, why not directly create a thread in the activity and save the creation of the service. Indeed, some scenarios can do this, but if your application is in the background, opens a thread to execute, and then is killed by the system. The task disappears halfway through, but the service is less likely to be killed by the system than thread and activity. In most cases, the service still exists. Of course, if you want the thread and activity to survive together, you don’t need to create a new service.

Then Android has a special thread called handlerthread, which inherits from thread and adds looper. For Android, it is easier to use than thread.

The official document recommends intentservice when talking about service, which is magical. It is equivalent to the handlerthread + service mentioned above, that is, you don’t need to add a thread in the service. Compared with service, it has the concept of work queue. For example, if you start intentservice four times, it will execute the next one in order, and then exit automatically after execution. And the tasks executed later may be affected by the tasks executed earlier. As shown below


If you go too far, open four and click close service immediately, he will finish the current task and close the service. As shown below


Here I am killed is printed in ondestory. This task actually takes five seconds to print, and then ends. In other words, although he will execute ondestory first, the tasks he is executing will still be completed. If you know this mechanism, you should pay attention not to create a dead circle here.

Then there is the interaction with the activity. Intentservice needs to be started with intent, so the data passed by the activity can be directly placed in intent. How does intentservice notify an activity? This aspect is the same as that of a service. I roughly looked for three kinds of binders, broadcasting and eventbus. Really lazy to write this, interested can look at my code.

Then look at the code here.

Intentservice is a service anyway, so startservice and stopservice are required.

Intent intent = new Intent(this,StudyIntentService.class);
//Start service
//Stop service

The intent can carry data.

Intentservice is relatively simple, just paste the code directly.

public class StudyIntentService extends IntentService {

    int i = 0;
     * @deprecated
    public StudyIntentService() {

    protected void onHandleIntent(@Nullable Intent intent) {try {
        } catch (InterruptedException e) {
            // Restore interrupt status.

    public void onDestroy() {
        Log.i("Tag","i am killed!");


A global variable I is defined here to prove that the same service is started when intentservice is started multiple times. Then there is a parameter intent in onhandleintent, where the data passed from the activity can be parsed and processed.

Then some life cycles can be familiar with by turning to documents, and there is nothing else to say.

Post the following project address:, if you don’t understand, you can look at the code directly. It should be easier to understand