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);