Multithreading: IntentService + Mind Map

Keywords: Java Android

Have a cup of coffee and relax!

Summary

IntentService uses

  1. Step 1: Define the IntentService class and override onHandleIntent
  2. Step 2: List file registration
  3. Step 3: Open Services

Step 1

class SIntentService : IntentService("SIntentService") {

    override fun onCreate() {
        Log.e("SIntentService", "onCreate___")
        super.onCreate()
    }

    override fun onStart(intent: Intent?, startId: Int) {
        Log.e("SIntentService", "onStart___")
        super.onStart(intent, startId)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.e("SIntentService", "onStartCommand___")
        return super.onStartCommand(intent, flags, startId)

    }

    override fun onHandleIntent(intent: Intent?) {
        Log.e("SIntentService", "onHandleIntent___"+intent?.getExtras()?.getString("task"))
        // According to the different Intent, different transactions are processed
        val string = intent?.getExtras()?.getString("task")
        when (string) {
            "task_1" -> Log.e("SIntentService", "task_1______");
            "task_2" -> Log.e("SIntentService", "task_2______");
        }

    }

    override fun onDestroy() {
        Log.e("SIntentService", "onDestroy___")
        super.onDestroy()
    }

}

Step 2

 <service android:name=".SIntentService"></service>

Step 3

   var i = Intent(mCtx, SIntentService::class.java);
   var bundle = Bundle();
   bundle.putString("task", "task_1");
   i.putExtras(bundle);
   startService(i)

   var i2 = Intent(mCtx, SIntentService::class.java);
   var bundle2 = Bundle();
   bundle2.putString("task", "task_2");
   i2.putExtras(bundle2);
   startService(i2)

Log log:

Log shows that when star Service () is opened many times, tasks will be inserted into the work queue by onStartCommand(), tasks will be executed sequentially, and services will be destroyed automatically.

Source code analysis

Source Mind Map:

In the mind map, we have drawn out what we have done in the corresponding function of IntentService.

make a concrete analysis

  • onCreate() source code

Main function: Initialize a Thread and Handler

  @Override
    public void onCreate() {
        super.onCreate();
     // step 1 opens a Thread instance (Handler Thread inherits Thread)
     HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
     thread.start();
     // step 2 retrieves the current Looper object through Thread
     mServiceLooper = thread.getLooper();
     // Step 3 Lopper and Handler Binding (Service Handler inherits Handler)
     mServiceHandler = new ServiceHandler(mServiceLooper);
    }

  • onStartCommand source code

Main role: insert Intent into the work queue

 @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        // Pass Intent into onStart (this is where Intent is queued when it is opened many times)
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }
  • onStart source code

Main function: Send Intent to Handler and leave it to handleMessage to process

    @Override
    public void onStart(@Nullable Intent intent, int startId) {
    //step 1 Gets Message Objects
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
    // step 2 uses the Hadler object initialized in the onCreate() function to send messages.
    // Intent is handled by handleMessage
        mServiceHandler.sendMessage(msg);
    }

ServiceHandler

    private final class ServiceHandler extends Handler {
        public ServiceHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
        //step 1 sends the Intent request down to onHandleIntent and processes it with the client
            onHandleIntent((Intent)msg.obj);
            // step 2 Stops Service
            stopSelf(msg.arg1);
        }
    }
  • onHandleIntent source code

Main role: need to be rewritten to process corresponding Intent requests

  @WorkerThread
    protected abstract void onHandleIntent(@Nullable Intent intent);
  • onDestroy source code

Main role: exit message queue

 @Override
    public void onDestroy() {
        mServiceLooper.quit();
    }

Analysis of the entire IntentService source code to the end of this level, if there is anything wrong, please point out!

If it is helpful to the guard, please give me a compliment. Thanks


Not a bit!

Posted by shatner on Mon, 07 Oct 2019 02:03:13 -0700