Handler and Multithreading application example in Android _android

Source: Internet
Author: User

This article first explains what handler is used for, and then introduces its application in multiple threads by example.

What is Handler

Handler is the process object used to send data between processes. In any process, you can send data to that process by using the Handler.sendmessage (message) method as long as you get a handler of another process. Based on this mechanism, we can create a new thread when dealing with multithreading, which has a handler in the UI thread. The UI thread updates the interface when thread processes time-consuming operations and sends data through handler like a UI thread.

Handler Application Multithreading Example

Our example is to implement a simple dictionary function. Applies a thread during the fetching of a Web page. The program consists of two activity, the first word to enter the query, and the second activity to display the result. We look directly at the contents of the second activity. (The first activity is simply to get the user input words and then pass to Activity2, not yet mastered to see my previous article)

First Look at OnCreate:

Copy Code code as follows:

@Override
protected 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 ()));

Declares a handler, its handler is the implementation of the callback class, where I inherited the activity
Callback, so you can pass in this.
Handler = new Handler (this);
Create a new thread (our own thread of implementation), and pass the above handler and query words to the constructor
Gethtmlthread thread = new Gethtmlthread (handler, Getintent ()
. Getstringextra ("query");
Start thread
Thread.Start ();

ProgressBar = (ProgressBar) Findviewbyid (R.ID.PRB);
Progressbar.setvisibility (view.visible);
}

Some of the necessary comments 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 inside itself, which is different from C #. Feeling or C # inside a good understanding. So let's take a look at this Gethtmlthread class:

Copy Code code as follows:

public class Gethtmlthread extends Thread
{

Private Handler Handler; Incoming handler.
String Querykey; The word to query

Public Gethtmlthread (Handler Handler, String Querykey)
{
This.handler = handler;
This.querykey = Querykey;
}

@Override
public void Run ()/thread-processed content
{
To get an explanation of the word on the Web page, return the content of the page
String a = gettranslatehtml (Querykey);

Defines a message that is used to send a handler handle to a UI thread
msg = new Message ();
Bundle Bundle = new Bundle ();
Put the results of the query into MSG
Bundle.putstring ("Answer", a);
Msg.setdata (bundle);
Set the identity of this msg so that the handler in the UI can be changed according to the UI
Msg.what = 0;
Send a message to handler processing in the UI
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:

Copy Code code as follows:

/**
* After inheriting the callback, this method must be implemented. That's where the handler's handling is done here.
*/
@Override
public boolean handlemessage (msg)
{
Judge Msg.what to determine which "event" to process
Switch (msg.what)
{
Case 0:
Progressbar.setvisibility (View.gone);
Take out the data from MSG and show it.
Bundle Bundle = Msg.getdata ();
Tvresult.settext (bundle.getstring ("answer"));
Break

Default
Break
}
return false;
}

This way we can see that when the second activity shows up for a while, the page does not show up, and there is no card death!

Finally, to comb the next step in the multithreading process:

Demo download

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.