1.定義dialog
private ProgressDialog dialog;
2.定義Handler對象
private Handler handler =new Handler(){
@Override
//當有消息發送出來的時候就執行Handler的這個方法
public void handleMessage(Message msg){
super.handleMessage(msg);
//只要執行到這裏就關閉對話框
dialog.dismiss();
}
};
3.實現代碼
private void processThread(){
//構建一個下載進度條
dialog= ProgressDialog.show(MainHandler.this, "Load", "Loading…");
new Thread(){
public void run(){
//在新線程裏執行長耗時方法
longTimeMethod();
//執行完畢後給handler發送一個空消息
handler.sendEmptyMessage(0);
}
}.start();