切换到主线程的几种方法

方法一: view.post(Runnable action)

textView.post(new Runnable() {
    @Override
    public void run() {
        textView.setText("更新textView");
        //还可以更新其他的控件
        imageView.setBackgroundResource(R.drawable.update);
    }
});

方法二: activity.runOnUiThread(Runnable action)

((MainActivity) context).runOnUiThread(new Runnable() {
    @Override
    public void run() {
    //此时已在主线程中,可以更新UI了
    }
});

方法三: Handler机制

Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
    @Override
    public void run() {
        //已在主线程中,可以更新UI
    }
});