IntentService source code for analysis is apI-25 version

IntentService belongs to the Service

IntentService inherits from Service, so it has the same structure as Service.



This needs no further explanation. But in use, it is a little different from Service, you can say a simplified version of Service. This is similar to the Handler/AsyTask relationship.

The use of the IntentService

Intent serviceIntent = new Intent(this, ParseIntentService.class);

startService(serviceIntent);

Copy the code

The above code starts an IntentService. Once it’s on, you don’t have to manage it. This is the simplified version of Service. ParseIntentService inherits IntentService and overrides the onHandleIntent() method.

IntentService source

IntentService has only a few hundred lines of source code



Parse the code here:

Looper and Handler appear.

  • ServiceHandler handleMessage(), after calling onHandleIntent(); StopSelf () stops the service. So IntentService doesn’t need to start the service because it stops the service after calling the handleMessage() method of ServiceHandler. We all know that handleMessage() is not called by the Handler itself, it is called when a message is sent. So now let’s look at where to send the message.

  • Looper will cover the introduction later

OnCreate () method of IntentService

We all know that services and activities alike have methods associated with their lifecycle. The onCreate() function of the Service is called when the door code starts the Service.



The following conclusions can be drawn from onCreat() :

  • HandlerThread A thread class starts a worker thread
  • MServiceLooper is the Looper of the worker thread
  • ServiceHandler is the Handler for the worker thread

    First of all,A ServiceHandler is a Looper for the worker thread. A ServiceHandler is a Looper for the worker thread. The main reason for determining which thread’s Handler is based on which thread the handlerMessage() method is executed.

    The secondMServiceLooper is a worker thread Looper.



    The screenshot shows the run() method of HandlerThread. You can see that mServiceLooper is obtained in run(), so mServiceLooper is the worker thread’s Looper.

    HandlerThread A thread classThat goes without saying.

HandlerThread

What does this class actually do? Simply put, it creates a worker thread Looper that polls our message distribution mechanism. . To learn more about handlerThreads, read the following article: Why and how to use handlerThreads?

Why Handler, Looper, the message distribution classes?



The ServiceHandler object was built above when onCreat() was created, mainly because of the message sent in the onStart() method of IntentService. Handle the abstract method onHandleIntent() via Handler message distribution mechanism.

Override the onHandleIntent() method using IntentService

  • First, create a class that inherits IntentService and implements onHandleIntent().
public class ParseIntentService extends IntentService {

public ParseIntentService(a) {

super("ParseIntentService");

}

/ * *

* This method is called on the worker thread

* * /


@Override

protected void onHandleIntent(@Nullable Intent intent) {

//TODO needs TODO something in the service

}

});

}

}

Copy the code
  • Second, create the Intent and call the startService() method.
Intent serviceIntent = new Intent(this, ParseIntentService.class);

startService(serviceIntent);

Copy the code

conclusion

  • The onHandleIntent() method is on the worker thread; You don’t have to update the UI
  • IntentService already has a worker thread in it and does not need to start a new worker thread. Note: If you’re calling onHandleIntent() with Retrofit and you’re using OkHttp, Service is recommended instead of IntentService. Because OkHttp has already created the worker thread for us. The worker thread that uses IntentService and uses IntentService is actually a memory drain.
  • This is a note to myself that the IntentService is the most important way to recognize the Handler in the worker thread; HandlerThread manages Looper for worker threads