前言

本文内容主要摘抄[Android子线程是否可以刷新UI],这里只做简单的总结,具体还是看参考文章,谢谢。

记录于此,只是方便自己查阅。

小结

  1. Android子线程(非UI线程)可以刷新UI,前提条件是有自己的ViewRoot。

  2. Activity的onCreate()周期时可以在子线程中刷新UI

  3. Activity的onResume()时才创建ViewRoot,此时是不可以在子线程刷新UI

  4. Activity的onStart()时在子线程可以刷新UI

正文

下面根据上面4点单独介绍(都是抄袭的哈,推荐看原文)。

第一点

Android子线程(非UI线程)可以刷新UI,前提条件是有自己的ViewRoot。

class NonUiThread extends Thread {
    @Override
    public void run() {
        Looper.prepare();
        TextView textView = new TextView(MainActivity.this);
        textView.setText("non-UiThread update textview");
        textView.setTextColor(Color.RED);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(50);
        textView.setBackgroundColor(Color.BLACK);
        WindowManager windowManager = MainActivity.this.getWindowManager();
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                400, 400, 0, 0, WindowManager.LayoutParams.FIRST_SUB_WINDOW,
                WindowManager.LayoutParams.TYPE_TOAST, PixelFormat.OPAQUE);
        windowManager.addView(textView, params);
        Looper.loop();
    }
}

亲测,可以正常显示。

第二点

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //子线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            ((Button) findViewById(R.id.main_bt_one)).setText("哈哈哈哈哈");
        }
    }).start();
}

正常显示。

第三点

是在onResume里面,对应ActivityThread就是handleResumeActivity这个方法。

final void handleResumeActivity(IBinder token,
            boolean clearHide, boolean isForward, boolean reallyResume) {
        // If we are getting ready to gc after going to the background, well
        // we are back active so skip it.
        unscheduleGcIdler();
        mSomeActivitiesChanged = true;

        // TODO Push resumeArgs into the activity for consideration
        ActivityClientRecord r = performResumeActivity(token, clearHide);
        ......
        if (r.window == null && !a.mFinished && willBeVisible) {
            r.window = r.activity.getWindow();
            View decor = r.window.getDecorView();
            decor.setVisibility(View.INVISIBLE);
            ViewManager wm = a.getWindowManager();
            WindowManager.LayoutParams l = r.window.getAttributes();
            a.mDecor = decor;
            l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
            l.softInputMode |= forwardBit;
            if (a.mVisibleFromClient) {
                a.mWindowAdded = true;
                wm.addView(decor, l);
            }

        // If the window has already been added, but during resume
        // we started another activity, then don't yet make the
        // window visible.
        } else if (!willBeVisible) {
            if (localLOGV) Slog.v(
                TAG, "Launch " + r + " mStartedActivity set");
            r.hideForNow = true;
        }
    ......
}

第四点

这个是参考文2最后提的问题。

activity.onStart()里通过线程刷新UI能成功吗?

我测试过,不管哪种启动模式,onStart()时子线程都可以刷新UI。

如果有问题,可以留言哈,多谢

参考文章

  1. Android子线程真的不能更新UI么

  2. Android子线程在没有ViewRoot的情况下能刷新UI吗?

  3. Activity的启动模式分析

相关文章

暂无评论

none
暂无评论...