Handler of android
The basic usage and operating principle of handler in android.
Recently, I watched the second quarter of the android reset version of mars's video about handler, which gave me a much better understanding of the components that used to know how to use them, but didn't quite understand the principle. Mars is really good, and the video is really good. Next I will write my learned knowledge and my understanding.
Basic Operating Principle of handler how does handler transmit data from worker thread to main thread handler how does it transmit data from main thread to post () method handler of worker thread handler
Why do we need a handler?I think this is the most important question, that is, what is the role of handler? Handler is mainly used for inter-thread communication. The question is, why does one app need multiple threads instead of only one main UI thread?
If there is only one main UI thread in the app, when network connection and network download are required, the android system detects that the main UI thread does not respond for a long time and sends an exception warning of application not response, called ANR. Therefore, in the main UI thread, it is necessary to interact with users in real time, and there cannot be a long wait problem. Therefore, these tasks must be performed in the worker thread.
However, according to android settings, threads other than the main UI thread (except for special controls such as progressbar) cannot modify the controls in the UI, which leads to new problems, how do we interact with users on the UI of the data obtained by the sub-thread? In this way, handler!
So how does handler run? The handler object first retrieves a message object and stores it in the messagequeue of a message queue. At the other end, there is a logoff object, cyclically retrieve the message object from the message queue and hand it to handler for processing.
How does handler transmit data from worker thread to main thread?
Public class MainActivity extends ActionBarActivity {private Handler handler; private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. cancleButton); // click the button to start the new thread button. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {Thread thread = new Mythread (); thread. start () ;}}); handler = new Myhandler () ;}// rewrite the handlerMessage method in hanlder, the class Myhandler extends Handler {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); String s = msg. obj. toString (); System. out. println ("in the current Thread" + Thread. currentThread (). getName () + "get message from this thread" + s) ;}// use handler in worker thread. the obtainMessage () method obtains a Message object, stores the message in the Message, and sends the class Mythread extends Thread {@ Override public void run () {Message msg = handler. obtainMessage (); msg. obj = currentThread (). getName (); handler. sendMessage (msg );}}}
In this example, we can see that the handlerMessage () method is running in the main thread, and he can get the message from the worker thread, you can update the message on the UI.
Why is the handleMessage method automatically called when a handler object is initialized? The reason is that there is a loose object in the android. OS. Handler class, which cyclically retrieves messages from the message queue and calls handleM, but returns a method.
How does handler transmit data from main thread to worker thread?
Public class MainActivity extends ActionBarActivity {private Handler handler; private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. cancleButton); // each time a button is clicked, a message button is sent to the worker thread. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {// use handler in main. the obtainMessage () method obtains a Message object, stores the message in the Message, and sends the Message msg = handler to the message queue. obtainMessage (); msg. obj = Thread. currentThread (). getName (); handler. sendMessage (msg) ;}}); Thread thread = new Mythread (); thread. start () ;}// rewrite the handlerMessage method in hanlder to process the obtained Message class Myhandler extends Handler {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); String s = msg. obj. toString (); System. out. println ("in the current Thread" + Thread. currentThread (). getName () + "get message from this thread" + s) ;}// work thread receives this message and calls handleMeesage () method to process message class Mythread extends Thread {@ Override public void run () {// The logoff object's loop method extracts messages from the message queue and calls the handleMeesage () method to process message logoff. prepare (); handler = new Myhandler (); loe. loop ();}}}
Through the running result, we can know that the handleMessage () method runs in the worker thread, so that the message can be processed in the worker thread to implement inter-process communication.
Post () method of handler
The post method generates a message, places the Runnable object in the message, puts the message in the message queue, and then retrieves the Runnable object in the main thread for execution in the main thread.
Public class MainActivity extends ActionBarActivity {private Handler handler; private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. cancleButton); // initialize the handler, and the processed Runnable Object handler = new Handler (); // click the button to execute the worker thread button. setOnClickListener (new OnClickListener () {public void onClick (View arg0) {Thread thread = new Mythread (); thread. start () ;}});} class Mythread extends Thread {@ Override public void run () {// initialize a Runnable object Runnable runnable = new Runnable () in worker thread () {public void run () {// print the running thread System of the Runnable method. out. println ("current Thread name" + Thread. currentThread (). getName () ;}}; // post the Runnable Object handler. post (runnable );}}}
The final result is that the Runnable object is executed in the main thread. With the post () method, we can directly generate a Runnable object in the worker thread, write the code for updating the UI in it, and update the UI directly using the post method.