After analyzing the Handler source code, we followed up with a source code analysis of the associated HandlerThread and IntentService.
HandlerThread
It is a subclass of Thread and comes with a Looper aura
- How to use:
Val HandlerThread = HandlerThread("mainActivity") handlerThread.start( Val myLooper: Looper = handlerThread. Looper val handler = object: Handler(myLooper) { override fun handleMessage(msg: Message?) Super.handlemessage (MSG) log. d("taonce", "handleMessage thread is: ${Thread.currentThread().name}") Log.d("taonce", "handleMessage msg is: ${msg?.arg1}") } } val message: Message = handler.obtainMessage() Message. Arg1 = 0x11 Message = handler.obtainMessage() msg.arg1 = 0x01 // handleMessage thread is: mainActivity // handleMessage msg is: 17 // handleMessage thread is: mainActivity // handleMessage msg is: 1Copy the code
through
Log
You can see that whether we send messages in the child thread or the main thread,handlerMessage(msg)
It’s all running onhandlerThread
In the. Moreover, thehandlerThread
In theLooper
Passed to thehandler
Later,handler
You don’t have to do it againLooper.prepare()
和Looper.loop()
The implementation of the process, eventuallyhandler
的handleMessage(msg: Message?)
Will be inhandlerThread
.
In other words:HandlerThread
A thread is opened to achieve multithreading effects, but it handles multi-tasks sequentially. - Source code brief analysis:
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; } Copy the code
Source,
run()
In this method, we do the default that we need to do in the child thread, so as not to use in the child threadHandler.sendMessage(msg: Message?)
causeNo LooperThe exception.
IntentService
-
How to use:
class MyIntentService : IntentService("") { override fun onHandleIntent(intent: Intent?) Thread.sleep(60 * 1000 * 1000)} Override fun onStart(intent: intent? , startId: Int) { super.onStart(intent, startId) } override fun onCreate() { super.onCreate() } }Copy the code
IntentService onHandleIntent(intent: Intent?) IntentService onHandleIntent(Intent: Intent?) IntentService (ANR) : IntentService (ANR) : IntentService (ANR) : IntentService (ANR)
-
Source code brief analysis:
// I removed some of the source code that was not analyzed, Public Abstract Class IntentService extends Service {// The Looper object in HandlerThread is private volatile Looper mServiceLooper; // Handler object private volatile ServiceHandler mServiceHandler; private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @override public void handleMessage(Message MSG) { onHandleIntent(@Nullable Intent intent) onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); } } @Override public void onCreate() { super.onCreate(); HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); MServiceLooper = thread.getLooper(); // Pass Looper to ServiceHandler mServiceHandler = new ServiceHandler(mServiceLooper); } @Override public void onStart(@Nullable Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; / / send a Message, and then in handleMessage (Message MSG) processing mServiceHandler. SendMessage (MSG); } @WorkerThread protected abstract void onHandleIntent(@Nullable Intent intent); }Copy the code
IntentService: IntentService: IntentService: IntentService
-
mServiceLooper
The variable, it’s aLooper
object -
mServiceHandler
Variable, it inheritsHandler
-
thread
是onCreate()
A local variable in a method is aHandlerThread
variable
Let’s start with a step by step analysis: In onCreate(), the thread variable is created and looper is retrieved, passed to the mServiceHandler object, Ensure that the serviceHandler handleMessage(Message MSG) executes the task in the newly opened thread.
In onStart(), use mServiceHandler to send a MSG, and when IntentService gets there, the mServiceHandler starts calling handlerMessage(Message MSG), In this case, the source code calls the abstract onHandlerIntent() method directly. This method will be overridden in our own custom IntentService to perform the time-dependent operation.
HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService: HandlerThread and IntentService
Scanning attention
-