Vi. use of--handler on the fifth day of Android learning

Source: Internet
Author: User

(Transferred from: http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html)

Vi. use of--handler on the fifth day of Android learning

Note: There are many functions that cannot be written in the activity, such as downloading or processing large amounts of data, if such actions are written in activity, it is possible that the entire activity will not respond or even error, so the user experience is poor. Therefore, such functions should be independent of the activity, and do not interfere with each other, so the handler was born. Handler provides an asynchronous message processing scheme that puts the specific complex operations in one thread and takes care of them.

The basic steps for creating a handler object are as follows:

(a) Click on a button, call handler's post method, the thread object will be executed to add to the queue.

(ii) The operation to be performed is written in the run () method of the Thread object.

(c) If you want to loop execution, you can execute handler's postdelayed or POST method inside the Run method, adding threads to the queue repeatedly.

Here is a simple example of using handler:

Package Mickey.handler;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;


public class Handlertest extends Activity {

Declaring a two button control
Private Button begin = null;
Private Button end = null;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Gets the object that represents the control based on the ID of the control and sets the appropriate listener for both buttons
Begin = (Button) Findviewbyid (R.id.begin);
Begin.setonclicklistener (New Startbuttonlistener ());
End = (Button) Findviewbyid (r.id.end);
End.setonclicklistener (New Endbuttonlistener ());

}

Listener set for the Start button
Class Startbuttonlistener implements Onclicklistener {

@Override
public void OnClick (View v) {
Without delay, join the message queue directly, at which point there is only one thread in the message queue
Call handler's Post method to add the thread object that will be executed to the queue
Handler.post (Updatethread);
}

}

Listeners set for the end button
Class Endbuttonlistener implements Onclicklistener {
@Override
public void OnClick (View v) {
Handler.removecallbacks (Updatethread);
}
}

Create a Handler object
Handler Handler = new Handler ();
A thread class, the operation to be performed writes the Run method of the Thread object
Runnable updatethread = new Runnable () {
@Override
public void Run () {
System.out.println ("Run ...");

Executes the postdelayed or POST method within the Run method, delays three seconds, joins the thread to the queue
Handler.postdelayed (Updatethread, 3000);
}
};

}

But we see that in the example above we just called the thread's Run () method and did not invoke the start () method of thread to restart a thread, so in the example above, we didn't actually restart a new thread, From the beginning to the end handler are all in the same thread as the activity.

So if we're going to restart a thread when we're using handler, using Handler.post (thread thread) just called the thread's Run method, we can only restart a new thread using the method of restarting the thread in Java. Call the Thread.Start () method, and there's no more talking here.

Vi. use of--handler on the fifth day of Android Learning (turn)

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.