Learning the Basic Usage of IntentService

Keywords: Android

What is IntentService?

Look at the source notes:
IntentService is a base class for {@link Service}s that handle asynchronousrequests (expressed as {@link Intent}s) on demand. Clients send requests through {@link android.content.Context#startService(Intent)} calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
IntentService is a subclass of Service that handles asynchronous requirements. Client starts the service through the startService(Intent) method. When the service is opened, it sends each intent to a queue of worker threads and stops automatically when all intent processing is complete.

Android has already provided two ways to use services. Why do you want to provide IntentService? Actually, this is an advantage of IntentService:

  1. First, you don't need to start the sub-threads yourself, because the Service runs on the main thread by default, processing time-consuming tasks block the main thread, while IntentService processing tasks are in the onHandleIntent method, you can see from the source that onHandleIntent has run on the onCreate() method and opened the sub-thread HandlerThread;
  2. Instead of shutting down the service yourself, automatically shut down after all intent s have been executed.

Steps to use IntentService:

  1. Create a MyIntentService that inherits from IntentService;
  2. Register this service in Mainfiest, and be careful to configure filters;
  3. Open the service in Activity.

Specific code:

Rewrite MyIntentService for IntentService:

 public MyIntentService() {
    super("MyIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    //Processing intent
    String first = intent.getExtras().getString("first");
    Log.e("LOG", "print data first=" + first);

    String second = intent.getExtras().getString("second");
    Log.e("LOG", "print data second=" + second);

    Log.e("LOG","-------------------------------------------------------------");
}

Declare and register intent filters within mainfest to open the service:

<service android:name=".MyIntentService">
        <intent-filter>
            <action android:name="intentservice.code.tom.intentservice"/>
        </intent-filter>
</service>

Open IntentService in Activity:

Intent intent = new Intent("intentservice.code.tom.intentservice");
intent.putExtra("first", "First Intent request");
startService(intent);

Intent intent1 = new Intent("intentservice.code.tom.intentservice");
intent1.putExtra("second", "The second Intent request");
startService(intent1);

Normally click once to open the IntentService Print log, instead of shutting down the service yourself, perform the shutdown yourself, where the onHandleIntent method executes twice:

04-09 10:18:03.206 6178-6178/intentservice.code.tom.intentservice E/LOG: onCreate
04-09 10:18:03.210 6178-6178/intentservice.code.tom.intentservice E/LOG: onStartCommand
04-09 10:18:03.210 6178-6493/intentservice.code.tom.intentservice E/LOG: print data first=First Intent request
04-09 10:18:03.210 6178-6493/intentservice.code.tom.intentservice E/LOG: print data second=null
04-09 10:18:03.210 6178-6493/intentservice.code.tom.intentservice E/LOG: -------------------------------------------------------------
04-09 10:18:03.220 6178-6178/intentservice.code.tom.intentservice E/LOG: onStartCommand
04-09 10:18:03.220 6178-6493/intentservice.code.tom.intentservice E/LOG: print data first=null
04-09 10:18:03.220 6178-6493/intentservice.code.tom.intentservice E/LOG: print data second=The second Intent request
04-09 10:18:03.220 6178-6493/intentservice.code.tom.intentservice E/LOG: -------------------------------------------------------------
04-09 10:18:03.223 6178-6178/intentservice.code.tom.intentservice E/LOG: onDestroy

What happens if you click more than once?This is consistent with the general Service approach, where the onCreate() method does not execute multiple times and onStartCommand() executes multiple times, looking at the log.

04-09 10:23:53.964 6178-6178/intentservice.code.tom.intentservice E/LOG: onCreate
04-09 10:23:53.973 6178-6178/intentservice.code.tom.intentservice E/LOG: onStartCommand
04-09 10:23:53.974 6178-6178/intentservice.code.tom.intentservice E/LOG: onStartCommand
04-09 10:23:53.974 6178-10613/intentservice.code.tom.intentservice E/LOG: print data first=First Intent request
04-09 10:23:53.974 6178-10613/intentservice.code.tom.intentservice E/LOG: print data second=null
04-09 10:23:53.974 6178-10613/intentservice.code.tom.intentservice E/LOG: -------------------------------------------------------------
04-09 10:23:53.975 6178-10613/intentservice.code.tom.intentservice E/LOG: print data first=null
04-09 10:23:53.975 6178-10613/intentservice.code.tom.intentservice E/LOG: print data second=The second Intent request
04-09 10:23:53.975 6178-10613/intentservice.code.tom.intentservice E/LOG: -------------------------------------------------------------
04-09 10:23:53.976 6178-6178/intentservice.code.tom.intentservice E/LOG: onDestroy

How does IntentService accomplish this?Take a look at the source code. IntentService's source code is simple:

  1. First IntentService opens a thread in the Oncreate() method, obtains the Looper, and creates the ServiceHandler object.
  2. Insert the message into the Looper queue using the ServiceHandler object in the onStart() method;
  3. In the handleMessage() method, onHandleIntent is executed and shuts itself down, but the service does not know what you are doing, so the onHandleIntent method is abstract and left to subclasses to implement.

Above is my simple understanding of IntentService. If you are not welcome to point out common progress ~~

Demo Download
This article is written in more detail

Posted by asparagus on Thu, 11 Jul 2019 09:56:59 -0700