Android 中切换回主线程的方法
1.view.post(Runnable action
textView.post(
new Runnable() {
@Override
public void run() {
textView.setText("更新tv");
//还可以更新其他的控件
imageView.setBackgroundResource(R.drawable.up2);
}
});
2. activity.runOnUiThread(Runnable action)
((MainActivity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
}
});
3. Handler机制
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
//已在主线程中,可以更新UI
}
});
FlowLiver Notes

浙公网安备 33010602011771号