I know a handler is bound to the thread's message queue in which it is created, but what if i create a handler in a service and posting a runnable declared in activity to which my service is bound. For example: In service:-
Handler handler=new Handler();
handler.post(Mainactivity.runnable);
In activity inside oncreate method:-
Runnable runnable=new Runnable(){
public void run{
//some code here
}
};
Can someone please explain me what's happening here?
Please find information about Message Queue and
Looperin below linkUnderstanding Android Core: Looper, Handler, and HandlerThread
In the Android via
Looper,Handler, andHandlerThread. The System can be visualized to be a vehicle as in the article’s cover.1.
MessageQueueis a queue that has tasks called messages which should be processed.2.
Handlerenqueues task in theMessageQueueusingLooperand also executes them when the task comes out of theMessageQueue.3.
Looperis a worker that keeps a thread alive, loops throughMessageQueueand sends messages to the corresponding handler to process.4 Finally Thread gets terminated by calling
Looper’squit() method.