1、利用handler传递消息:
①子线程中发出消息,通知Handler完成UI更新:
new Thread(){
@Override
public void run() {
//省略耗时操作,完成之后发送消息给Handler,完成UI更新;
Message msg = Message.obtain();
msg.what = SHOW_UPDATE_DIALOG;
handler.sendMessage(msg);
}
}.start();
②handler方法:
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg){
switch(msg.what){
case SHOW_UPDATE_DIALOG:
//提示用户更新
String desc = (String)msg.obj;
//更新UI
ll_loading.setVisibility(View.INVISIBLE);
break;
default:
break;
}
}
};
2.直接在子线程中调用runOnUiThread更新UI
new Thread(){
@Override
public void run() {
//省略耗时操作,完成之后发送消息给Handler,完成UI更新;
runOnUiThread(new Runnable(){
@Override
public void run() {
ll_loading.setVisibility(View.INVISIBLE);
}
});
}
}.start();