Android Handler calls the Post method to block

Source: Internet
Author: User

1. Try using postdelayed (Runnable A, int time), because post puts the message in Looper and returns, but there is no other message in the looper that will be taken out immediately, so it is possible to do the operation in the run, instead of refreshing the button in time.

2. In addition, the time-consuming operation of this practice is still done by the UI thread: Instead of you think of another thread.
It is advisable to use the following method:
Defines a thread.
Class MyThread extends thread{
Handler Mhandler;
Boolean Boo;
Public MyThread (Handler Handler) {
Mhandler = handler;
}
public void Setboo (Boolean b) {boo = b;}
Publid void Run () {
if (boo) {
Getweatherinfo ();//time-consuming operation
Analyzing ();//time-consuming operation
Mhandler.post (New Runnable () {
public void Run () {
Setweather ();//Update UI
}
);//Update UI
Boo = true;
}
}
}

When you handle a click event:
Surebutton.setonclicklistener (New Button.onclicklistener () {
public void OnClick (view view) {
Setboo (TRUE);
Mythread.start ();
}
});

In the activity:
MyThread MyThread = new MyThread (Mhandler);

Android Handler calls the Post method to block

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.