android handler調用post方法阻塞


1.試下用postDelayed(Runnable a, int time),因為post把消息放到Looper中就返回,但Looper中沒有其他消息又會被立刻取出來執行,這樣就有可能做了run中的操作,而沒有及時刷新按鈕.

2.另外,這種做法耗時操作仍然是由 UI線程去做了。。而不是你想的另起了線程。。
建議最好用下面的方法:
定義一個線程。
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();//耗時操作
analyzing();//耗時操作
mHandler.post(new Runnable() {
public void run() {
setWeather();//更新 UI
}
);//更新 UI
boo = true;
}
}
}

在處理單擊事件時:
sureButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View view){
setBoo(true);
myThread.start();
}
});

在activity中:
MyThread myThread = new MyThread(mHandler);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM