Android journey-Handler and Multithreading

Source: Internet
Author: User

This article first explains what handler is used for, and then introduces its application in multithreading through examples.

What is Handler?

Handler is generally used to send data processing objects between processes. In any process, as long as the handler of another process is obtained, data can be sent to that process through the handler. sendMessage (message) method. Based on this mechanism, we can create a thread when processing multiple threads, which has a handler in the UI thread. After processing some time-consuming operations, the thread sends data through the passed handler like the ui thread, and the UI thread updates the interface.

 

Example of handler application Multithreading

 

In this example, we implement a simple dictionary function. Apply the thread when obtaining the webpage. There are two activities in this program. The first is used to enter the query word, and the second is used to display the result. Let's look at the content of the second activity. (The first activity is nothing more than getting the words entered by the user and passing them to activity2. If you have not mastered the first activity, you can read the previous article)

First, let's look at OnCreate:

@ Overrideprotected void onCreate (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 whose handler implements the callback class. Here I inherit the activity // callback, so this can be passed in. Handler = new Handler (this); // create a thread (our own thread) and pass the preceding handler and query words into the constructor GetHtmlThread thread = new GetHtmlThread (handler, getIntent (). getStringExtra ("query"); // start thread. start (); progressBar = (ProgressBar) findViewById (R. id. prb); progressBar. setVisibility (View. VISIBLE );}

I have already noted some necessary comments in the code. The GetHtmlThread Thread in the Code inherits from a class of the Thread and implements the run method in it. This is different from that in c. I think it's better to understand c. Let's take a look at the GetHtmlThread class:

 

Public class GetHtmlThread extends Thread {private Handler handler; // input handlerString queryKey; // the word to be queried public GetHtmlThread (Handler handler, String queryKey) {this. handler = handler; this. queryKey = queryKey ;}@ Overridepublic void run () // content processed by the thread {// obtain the explanation of the word in the webpage. The returned webpage content is String a = GetTranslateHtml (queryKey ); // define a Message for handler sending the Message msg = new Message () to the UI thread; Bundle bundle = new Bundle (); // put the query result into the msg bundle. putString ("answer", a); msg. setData (bundle); // set the msg identifier so that the handler in the UI can change the corresponding UImsg according to this. what = 0; // send the message to handler in the UI to process handler. 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 callback, you must implement this method. That is to say, the handler above is processed here * // @ Overridepublic boolean handleMessage (Message msg) {// judge msg. to determine which "Event" to process switch (msg. what) {case 0: progressBar. setVisibility (View. GONE); // extract the data in msg and display Bundle bundle = msg. getData (); tvResult. setText (bundle. getString ("answer"); break; default: break;} return false ;}

 

In this way, we can see that the web page is displayed only after the second activity is displayed for a period of time, and the page is not stuck!

Finally, let's sort out the multi-thread processing steps:

 

 

Download Demo

 

Download Demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.