This article first explains what handler is for, and then gives an example of its application in multi-threading.
What is Handler
Handler popular point is the processing object used to send data between processes. In any process, you can send data to that process through the handler.sendmessage (message) method as long as the handler of another process is obtained. Based on this mechanism, we can create a new thread when dealing with multithreading, which has a handler in the UI thread. When thread finishes processing some time-consuming operations, it sends the data through the handler like the UI thread, and the UI thread updates the interface.
Handler Application Multithreading Example
Our example is to implement a simple dictionary function. The thread is applied during the Fetch Web page. This program has two activity, the first word to enter the query, and the second activity to display the results. We look directly at the contents of the second activity. (The first activity is simply to get the word entered by the user and pass it on to Activity2, which is not yet available to read my previous article)
First Look at OnCreate:
@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.queryresult); Tvresult=(TextView) Findviewbyid (R.id.tvresult); String queryString= Getintent (). Getstringextra ("Query"); LOG.D ("threadId:", String.valueof (Thread.CurrentThread (). GetId ())); //declare a handler, its handler is the class that implements the callback, here I inherit the activity//callback, so you can pass in this. Handler =NewHandler ( This); //Create a new thread (a thread of our own implementation) and pass the above handler and queried words into the constructorGethtmlthread thread =NewGethtmlthread (Handler, Getintent (). Getstringextra ("Query")); //Start ThreadThread.Start (); ProgressBar=(ProgressBar) Findviewbyid (R.ID.PRB); Progressbar.setvisibility (view.visible); }
Some of the necessary notes I have already indicated in the code. The Gethtmlthread thread in the code is a class that inherits from thread, and then implements the Run method itself, which is not the same as in C #. It feels good to understand a little in C #. Then let's take a look at this Gethtmlthread class:
Public classGethtmlthread extends Thread {PrivateHandler Handler;//the incoming handlerString Querykey;//the word to query PublicGethtmlthread (Handler Handler, String querykey) { This. Handler =handler; This. Querykey =Querykey; } @Override Public voidRun ()//thread-Processed content { //get an explanation of the word on the Web page, return the page contentString A =gettranslatehtml (Querykey); //defines a message that is used to send handler processing to the UI threadMessage msg =NewMessage (); Bundle Bundle=NewBundle (); //put the results of the query into the MSGBundle.putstring ("Answer", a); Msg.setdata (bundle); //set the ID of this msg so that handler in the UI can change the corresponding UI based on thisMsg.what =0; //sending messages to handler processing in the UIhandler.sendmessage (msg); Super.run (); } }
Now the messages in the thread are sent out. We then process the message in the UI thread. The code is as follows:
/** * After inheriting the callback, this method must be implemented. Which is where the handler is handled.*/@Override Publicboolean handlemessage (Message msg) {//Judge Msg.what to determine which "event" is to be dealt with. Switch(msg.what) { Case 0: Progressbar.setvisibility (View.gone); //take out the data in the MSG and show it .Bundle bundle =Msg.getdata (); Tvresult.settext (Bundle.getstring ("Answer")); Break; default: Break; } return false; }
So we can see that when the second activity shows up for a while, the page doesn't show up, and it doesn't get stuck!
Finally, we will comb the steps of Multithreading:
Android Handler and multithreading