Android探索更新UI的方法(二)-更新UI的方式

前言

  前一章我们讲了更新UI的时机,即Android是在哪一刻才更新UI的,了解Android更新UI的源码流程有助于我们了解其本质,在开发过程中出错也能更快的定位。当然看源码也要不求甚解,不要一头砸进去,毫无头绪,只需顺着一条主线,适可而止。像UI怎么显示到屏幕上的,实际App进程是把显示操作发给System_Server进程的WindowManagerService线程,让它去显示,中间通过Binder线程实现进程间交互。

Android更新UI的几种方式

  我们知道Android中有以下几种更新UI的方式,我们探讨的都是在非UI线程更新,下同。

        1、Handler sendMessage() or post();
        2、Activity runOnUiThread();
        3、View post()
        4、AsyncTask
        5、View其他相关用法

Handler

  handler是最常用的,也是Android中最基本的方法,Android中所有的异步更新UI的本质都是用Handler。那么我们看一下sendMessage和post有什么区别

public final boolean sendMessage(Message msg)
{
        return sendMessageDelayed(msg, 0);
}

public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
}

发现最后调用的是sendMessageAtTime,这个方法里面吧Message加入到MessageQueue里面

public final boolean post(Runnable r)
{
       return  sendMessageDelayed(getPostMessage(r), 0);
}

private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
}

其实就是把Runnable包装成一个Message,即把Message的callback设置成Runnable,之后就会在主线程回调Runnable的run方法。就这么简单实现了UI的更新。

runOnUiThread()

  runOnUiThread()是Activity的方法

// we must have a handler before the FragmentController is constructed
final Handler mHandler = new Handler();

/**
     * Runs the specified action on the UI thread. If the current thread is the UI
     * thread, then the action is executed immediately. If the current thread is
     * not the UI thread, the action is posted to the event queue of the UI thread.
     *
     * @param action the action to run on the UI thread
     */
    @Override
    public final void runOnUiThread(Runnable action) {
        if (Thread.currentThread() != mUiThread) {
            mHandler.post(action);
        } else {
            action.run();
        }
    }

  如果是在主线程调用该方法,则跟没调用一样的,都是立即更新UI,调用Runnable的run方法;
  如果实在子线程调用该方法,则通过handler.post()把它放进消息队列。所以这个方法的本质很简单,主线程立即更新,子线程通过handler放进MessageQueue
  注意mHandler是Activity里面的handler,mUiThread实在Activity的attach方法中赋值的,这个方法是由ActivityThread经过一系列方法调用的,所以这个是主线程。

View post()

Android还提供这个View.post()方法来供我们更新UI,这个方法在View.java里面

/**
     * <p>Causes the Runnable to be added to the message queue.
     * The runnable will be run on the user interface thread.</p>
     *
     * @param action The Runnable that will be executed.
     *
     * @return Returns true if the Runnable was successfully placed in to the
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.
     *
     * @see #postDelayed
     * @see #removeCallbacks
     */
    public boolean post(Runnable action) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().post(action);
        return true;
    }

  从注释可以知道,当attachInfo为空时,即这时候UI还没创建好,是吧Runnable添加到队列中;当attachInfo不为空时,它是把Runnable投递到主线程的消息队列,这里用的handler是mAttachInfo的handler,我们看一下它是在哪,什么时候创建的。

    AttachInfo mAttachInfo;
/**
     * Queue of pending runnables. Used to postpone calls to post() until this
     * view is attached and has a handler.
     */
    private HandlerActionQueue mRunQueue;

/**
     * @param info the {@link android.view.View.AttachInfo} to associated with
     *        this view
     */
    void dispatchAttachedToWindow(AttachInfo info, int visibility) {
        mAttachInfo = info;
        .....
        // Transfer all pending runnables.
        if (mRunQueue != null) {
            mRunQueue.executeActions(info.mHandler);
            mRunQueue = null;
        }
    }

dispatchAttachedToWindow()是在ViewRootImpl中调用的

ViewRootImpl.java

private void performTraversals() {
        // cache mView since it is used so much below...
        final View host = mView;
        .....
        if (mFirst) {
                host.dispatchAttachedToWindow(mAttachInfo, 0);
        }
        // Execute enqueued actions on every traversal in case a detached view enqueued an action
        getRunQueue().executeActions(mAttachInfo.mHandler);
        .....
}

mView是在setView()中赋值的

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
        synchronized (this) {
            if (mView == null) {
                mView = view;
            }
       }
}

而setView是WindowManagerGlobal中调用的,关于WMS等的源码分析我们之后再讲,这里只需要重点关注View的mRunQueue,也就是在Activity启动的流程中,会把post的Runnable放到mRunQueue这个队列里,当view显示到屏幕上,会调用ViewRootImpl的performTraversals(),然后调用View的dispatchAttachedToWindow,然后执行mRunQueue里面的任务。
  总结:当view没创建好时,先把任务添加到队列中,不会执行;只有当view创建完毕,显示到屏幕上时,才回去调用任务队列里面的任务,所以我们在onCreate(),onStart()等生命周期里面post的任务不会立即执行,有兴趣的童鞋可以去试一下。
  接下来我们重点关注mRunQueue这个HandlerActionQueue

/**
 * Class used to enqueue pending work from Views when no Handler is attached.
 *
 * @hide Exposed for test framework only.
 */
public class HandlerActionQueue {
    private HandlerAction[] mActions;
    private int mCount;

    public void post(Runnable action) {
        postDelayed(action, 0);
    }

    public void postDelayed(Runnable action, long delayMillis) {
        final HandlerAction handlerAction = new HandlerAction(action, delayMillis);

        synchronized (this) {
            if (mActions == null) {
                mActions = new HandlerAction[4];
            }
            mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
            mCount++;
        }
    }

    public void removeCallbacks(Runnable action) {
        synchronized (this) {
            final int count = mCount;
            int j = 0;

            final HandlerAction[] actions = mActions;
            for (int i = 0; i < count; i++) {
                if (actions[i].matches(action)) {
                    // Remove this action by overwriting it within
                    // this loop or nulling it out later.
                    continue;
                }

                if (j != i) {
                    // At least one previous entry was removed, so
                    // this one needs to move to the "new" list.
                    actions[j] = actions[i];
                }

                j++;
            }

            // The "new" list only has j entries.
            mCount = j;

            // Null out any remaining entries.
            for (; j < count; j++) {
                actions[j] = null;
            }
        }
    }

    public void executeActions(Handler handler) {
        synchronized (this) {
            final HandlerAction[] actions = mActions;
            for (int i = 0, count = mCount; i < count; i++) {
                final HandlerAction handlerAction = actions[i];
                handler.postDelayed(handlerAction.action, handlerAction.delay);
            }

            mActions = null;
            mCount = 0;
        }
    }

    public int size() {
        return mCount;
    }

    public Runnable getRunnable(int index) {
        if (index >= mCount) {
            throw new IndexOutOfBoundsException();
        }
        return mActions[index].action;
    }

    public long getDelay(int index) {
        if (index >= mCount) {
            throw new IndexOutOfBoundsException();
        }
        return mActions[index].delay;
    }

    private static class HandlerAction {
        final Runnable action;
        final long delay;

        public HandlerAction(Runnable action, long delay) {
            this.action = action;
            this.delay = delay;
        }

        public boolean matches(Runnable otherAction) {
            return otherAction == null && action == null
                    || action != null && action.equals(otherAction);
        }
    }
}

它里面有个内部类HandlerAction,封装了Runnable,HandlerAction[]保存了任务,初始容量大小为4。可以看到post()方法就是把Runnable放进handlerAction数组里面,并没有执行,真正的执行在dispatchAttachedToWindow方法中

if (mRunQueue != null) {
            mRunQueue.executeActions(info.mHandler);
            mRunQueue = null;
}

public void executeActions(Handler handler) {
        synchronized (this) {
            final HandlerAction[] actions = mActions;
            for (int i = 0, count = mCount; i < count; i++) {
                final HandlerAction handlerAction = actions[i];
                handler.postDelayed(handlerAction.action, handlerAction.delay);
            }

            mActions = null;
            mCount = 0;
        }
}

执行就是遍历HandlerAction数组,依次执行,这里的handler是AttachInfo的handler,它是ViewRootImpl中的ViewRootHandler,它也是继承Handler的。执行完毕后清空HandlerAction数组。

view post 时序图

UML时序图.png

AsyncTask

AsyncTask的原理其实也是用Handler更新UI的,它内部有一个InternalHandler sHandler;

private static class InternalHandler extends Handler {
        public InternalHandler(Looper looper) {
            super(looper);
        }

        @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
        @Override
        public void handleMessage(Message msg) {
            AsyncTaskResult<?> result = (AsyncTaskResult<?>) msg.obj;
            switch (msg.what) {
                case MESSAGE_POST_RESULT:
                    // There is only one result
                    result.mTask.finish(result.mData[0]);
                    break;
                case MESSAGE_POST_PROGRESS:
                    result.mTask.onProgressUpdate(result.mData);
                    break;
            }
        }
    }

注意它初始化Handler用的是主线程的Looper,sHandler = new InternalHandler(Looper.getMainLooper());关于AsyncTask的工作原理之后会讲到。

View其他相关用法

ViewTreeObserver

  在开发中,通常有这样一种需求,我们需要获取View的宽高等信息,要获取这些信息必须等View完成measure后才能获取,可以用上面我们将的View的post获取,也可以用ViewTreeObserver设置监听来获取

        final ViewTreeObserver observer = surfaceView.getViewTreeObserver();
        ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (observer.isAlive()) {
                    observer.removeGlobalOnLayoutListener(this);
                    //获取宽高 
                }
            }
        };
        observer.addOnGlobalLayoutListener(layoutListener);

它会在View的layout发生变化时调用,注意可能会多次调用,用过一次后最好移除监听,否则回调会多次执行,拿它是什么时候回调的呢,我们要再一次进入ViewRootImpl的performTravels()方法

ViewTreeObserver.java

public final void dispatchOnGlobalLayout() {
        final CopyOnWriteArray<OnGlobalLayoutListener> listeners = mOnGlobalLayoutListeners;
        if (listeners != null && listeners.size() > 0) {
            CopyOnWriteArray.Access<OnGlobalLayoutListener> access = listeners.start();
            try {
                int count = access.size();
                for (int i = 0; i < count; i++) {
                    access.get(i).onGlobalLayout();
                }
            } finally {
                listeners.end();
            }
        }
}

ViewRootImpl.java

private void performTraversals() {
        .....
        performMeasure();
        .....
        performLayout();

        if (triggerGlobalLayoutListener) {
            mAttachInfo.mRecomputeGlobalAttributes = false;
            mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
        }
        .....
        performDraw();
        .....
}

当View完成测量,布局后,如果设置了监听,则回调。所以这也能正确的获取宽高。

MessageQueue.IdleHandler

在上一节中,我们提到过MessageQueue中有个有趣的接口叫IdleHandler

private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>();
private IdleHandler[] mPendingIdleHandlers;
/**
     * Callback interface for discovering when a thread is going to block
     * waiting for more messages.
     */
    public static interface IdleHandler {
        /**
         * Called when the message queue has run out of messages and will now
         * wait for more.  Return true to keep your idle handler active, false
         * to have it removed.  This may be called if there are still messages
         * pending in the queue, but they are all scheduled to be dispatched
         * after the current time.
         */
        boolean queueIdle();
    }

public void addIdleHandler(@NonNull IdleHandler handler) {
        if (handler == null) {
            throw new NullPointerException("Can't add a null IdleHandler");
        }
        synchronized (this) {
            mIdleHandlers.add(handler);
        }
}

public void removeIdleHandler(@NonNull IdleHandler handler) {
        synchronized (this) {
            mIdleHandlers.remove(handler);
        }
}

Message next() {
        .....
        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        for (;;) {
                if (pendingIdleHandlerCount < 0
                        && (mMessages == null || now < mMessages.when)) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                if (pendingIdleHandlerCount <= 0) {
                    // No idle handlers to run.  Loop and wait some more.
                    mBlocked = true;
                    continue;
                }

                if (mPendingIdleHandlers == null) {
                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                }
                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
        }

        // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            for (int i = 0; i < pendingIdleHandlerCount; i++) {
                final IdleHandler idler = mPendingIdleHandlers[i];
                mPendingIdleHandlers[i] = null; // release the reference to the handler

                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log.wtf(TAG, "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized (this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            // Reset the idle handler count to 0 so we do not run them again.
            pendingIdleHandlerCount = 0;
        .....
}

通过官方解释我们可以了解这个接口的作用:当消息队列中没有消息时,会回调该接口,前提是你设置了监听。它的返回值:返回false,那么就会移除它,返回true就会在下次message处理完了的时候继续回调。

那么这个接口到底有什么用呢,其实也可以用来获取View的宽高,在View初始化,Activity启动的时候,MessageQueue的有许多的消息,我们可以从ViewRootImpl的ViewRootHandler里面可以看出。当MessageQueue为空时,表示View真正的完成了measue,layout,draw等操作,这是我们就可以去执行一些获取view属性的操作或者做一些耗时操作,这时就不会影响UI的显示了,特别是进入App主页,经常需要向服务器请求大量的数据来更新UI,如果子线程过多,又设置了过高的优先级,就会抢占调度UI线程,导致卡顿,所以这个接口就很有用了。那么怎么具体使用呢?

         //MessageQueue queue = Looper.getMainLooper().getQueue();
        MessageQueue queue = Looper.myQueue();
        queue.addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                Log.d("Activity","idleHandler");
                return false;
            }
        });
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容

  • 【Android Handler 消息机制】 前言 在Android开发中,我们都知道不能在主线程中执行耗时的任务...
    Rtia阅读 4,806评论 1 28
  • 童话大王郑渊洁说,他的母亲是一个有个性的人,从小给他讲自己编的故事,一个故事讲了好几年,大意是动物们过河,一大群动...
    宛若星宸阅读 375评论 1 1
  • 两个人的爱靠什么维持,时间、金钱、感情,都是不可或缺的,可为什么感情在最后,因为它脆弱,脆弱的经不起往往时间和金钱...
    想多了吧你阅读 232评论 0 1
  • 1. 某某人,根据你的自我定义,你觉得怎样的自己才是值得被爱的?为什么? 根据我的定义我觉得把所有人比下去我才是值...
    小雪花M阅读 206评论 0 0
  • 这段时间倾注了很多心血。全心全意的来做所谓的事业。不管多忙、多忙炎热,总是坚持聚集到一起,煞有介事地来讨论所谓事业...
    家里人家阅读 98评论 0 0