Android能否在子线程刷新UI

Android都需要在主线程刷新UI吗?(以下环境在android5.1中分析)

在子线程刷新UI一般会有这样的异常:CalledFromWrongThreadException
该异常来自于framework/base/core/java/android/view/ViewRootImpl.java中的
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException(
“Only the original thread that created a view hierarchy can touch its views.”);
}
}
其中在ViewRootImpl的构造中会发现:
public

ViewRootImpl(Context context, Display display) {
mContext = context;

mWindowSession = WindowManagerGlobal.getWindowSession();
mDisplay = display;
mBasePackageName = context.getBasePackageName();
mDisplayAdjustments = display.getDisplayAdjustments();
mThread = Thread.currentThread();

...

}
而每次在更新View的时候都会调用checkThread方法来检测线程情况,因此如果需要在子线程中刷新UI的话则需要在子线程中创建ViewRoot(5.1中是ViewRootImpl)了,但是ViewRootImpl并不能直接new,它会在哪里创建呢?我们可以看到类:android.view.WindowManagerImpl该
类实现 WindowManager,其中在addView的时候如下:
public void

addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultToken(params);
mGlobal.addView(view, params, mDisplay, mParentWindow);
}
进入WindowManagerGlobal找到addView方法:发现这么个关键地方
public void addView(View view, ViewGroup.LayoutParams params,Display display, Window parentWindow) {

ViewRootImpl root;

root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
}
到此我们知道了他们关联的地方,那么如果想要在子线程更新的话也就是只需要在子线程addView就可以了,测试例子如下:
public class MainActivity extends Activity implements OnClickListener
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.m_btn_show).setOnClickListener(this);
    findViewById(R.id.m_btn_hide).setOnClickListener(this);
}

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    switch (v.getId())
    {
        case R.id.m_btn_show:
            test();
            break;
        case R.id.m_btn_hide:
            mHandler.sendEmptyMessage(1);
            break;
    }
}

private Handler mHandler;

private void test()
{
    new Thread()
    {

        @Override
        public void run()
        {
            // TODO Auto-generated method stub
            super.run();
            Looper.prepare();
            final TextView tx = new TextView(MainActivity.this);
            tx.setText("test11111111111111111");
            final WindowManager wm = MainActivity.this.getWindowManager();

// WindowManager.LayoutParams params =
// new WindowManager.LayoutParams(250, 250, 200, 200, WindowManager.LayoutParams.FIRST_SUB_WINDOW, WindowManager.LayoutParams.TYPE_TOAST, PixelFormat.OPAQUE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.flags = LayoutParams.FLAG_NOT_FOCUSABLE|LayoutParams.FLAG_NOT_TOUCHABLE;
params.width = 200;
params.height = 200;
params.x = 100;
params.y = 100;
params.type = LayoutParams.FIRST_SUB_WINDOW;
wm.addView(tx, params);
mHandler = new Handler()
{

                @Override
                public void handleMessage(Message msg)
                {
                    // TODO Auto-generated method stub
                    super.handleMessage(msg);
                    wm.removeView(tx);
                }

            };
            Looper.loop();
        }

    }.start();
}

}
从这里也可以看出Android视图和线程的关系。
问题:为什么addView过程(也就是new ViewRootImpl)的时候要放在Looper.prepare()和Looper.loop()之间?
因为在ViewRootImpl的构造中有创建Handler:
mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);

你可能会注意到这里的addView中有
Display,这是干嘛的?查看以下引用发现如下:
Android能否在子线程刷新UI_第1张图片

其实它表示显示的载体(可以在多屏幕情况下使用)

你可能感兴趣的:(Android,狗狗吃骨头)