Toast显示超详细过程

前言

上次看到有人说Toast属于UI操作,只能在主线程中使用。其实不是这样的,Toast是特殊的UI操作,由系统管理,我们只需在线程中创建MessageQueue和Looper让Toast能够回调当前线程即可在任意线程使用,主线程由于属于ActivityThread,默认创建了消息队列,所以可以直接使用Toast,Thread中则需要:

Looper.prepare();
Toast.makeText(context,text,duration).show();
Looper.loop();

注意:

  1. 文中大量涉及到进程间通讯机制Binder的使用,建议先看些Binder的资料
  2. 文中源码过长的有省略,想要阅读源码的朋友可以在这两个网站上查看:1、http://androidxref.com 2、 http://www.grepcode.com/

下面结合源码详细介绍一下Toast的显示之路,为了方便理解,先展示UML图:

总结

因为每个Toast显示时间可能冲突,因此需要一个管理者来统一管理他们,在一个队列中一个一个显示,所以要使用管理者(NMS)的binder引用将Toast入队,显示又要调用使用线程的binder引用,因此Toast的显示大量用到了进程间通信。

  1. 在使用线程中调用Toast.makeText(),创建一个Toast实例
  2. 调用toast.show();在这个方法中又调用getService()获取NotificationManagerService的binder引用,将toast和mTN(继承自ITransientNotification.Stub的TN类的实例,用来提供给NMS一个Binder引用,需要显示Toast时可以调用TN的方法)等信息enqueue(),插入NMS中的待显示序列。
  3. 轮到某个Toast显示时,在NMS中调用TN类的show()方法,在show方法中又通过handler在使用线程中直接获取WindowManagerImpl然后addView,将Toast添加在Window中。
  4. 显示的同时NMS也发送了一个延时消息,显示时间过后就会调用TN的hide()方法,通过handler在使用线程中调用WindowManagerImpl的removeVIew()方法删除该Toast
序列图.png

源码分析

Toast的创建

创建Toast我们都会,最简单的方法就是 makeText();
看一下源码:

public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
        //创建一个Toast
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        
        //Toast中要显示的TextView
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);
        result.mNextView = v;
        //Toast显示的时间
        result.mDuration = duration;

        return result;
    }

重要语句已注释,现在我们知道在调用makeText时返回值就是创建完毕的Toast。下面看show()方法。

加入NMS的Toast队列

系统中有可能很多程序都想要显示Toast,如果同时出现就失去了原有的作用,因此必须有一个管理者来统一管理所有需要显示的Toast,这个管理者就是系统服务之一:NotificationManagerService

 public void show() {
        if (mNextView == null) {
            throw new RuntimeException("setView must have been called");
        }
        //获取NotificationManagerService的客户端
        INotificationManager service = getService();
        
        String pkg = mContext.getOpPackageName();
        
        //tn是系统将会调用的回调,来真正实现将Toast显示在当前Window
        TN tn = mTN;
        tn.mNextView = mNextView;

        try {
            service.enqueueToast(pkg, tn, mDuration);
        } catch (RemoteException e) {
            // Empty
        }
    }

看到这里,我们发现了两个重要的类,INotificationManager和TN。

INotificationManager:(AIDL声明的接口,具体实现在INotificationManager.Stub里,而系统服务NotificationManagerService正式继承了INOtificationManager.Stub)是系统服务NMS在本地的binder引用,也就是客户端,我们要跨进程调用NMS的方法就要用到它。获取方法:
      
static private INotificationManager getService() {
    if (sService != null) {
        return sService;
    }
    sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
    return sService;
}

TN:是真正实现将Toast显示到当前Window的工具,它继承自ITransientNotification.Stub,也就是一个服务端,NMS可以调用TN的binder引用来调用TN的show()和hide()方法来显示隐藏Toast。

通过调用NotificationManagerService的enqueue()方法,很明显是要把包名,回调、显示时间发送过去统一排队,一个一个显示。下面看看排队时发生了什么

NMS回调TN

排队时发生了什么呢,贴上源码:重要的就是这个同步的部分,前面省略

    synchronized (mToastQueue) {
            //这里获取请求线程的pid和Uid
            int callingPid = Binder.getCallingPid();
            long callingId = Binder.clearCallingIdentity();
            try {
                ToastRecord record;
                int index = indexOfToastLocked(pkg, callback);
                // If it's already in the queue, we update it in place, we don't
                // move it to the end of the queue.
                //英文注释很清楚了,这个index表示这条Toast是不是已经存在在队列中且尚未显示,如果是,则刷新。
                if (index >= 0) {
                    record = mToastQueue.get(index);
                    record.update(duration);
                } else {
                    // Limit the number of toasts that any given package except the android
                    // package can enqueue.  Prevents DOS attacks and deals with leaks.
                    if (!isSystemToast) {
                        int count = 0;
                        final int N = mToastQueue.size();
                        for (int i=0; i<N; i++) {
                             final ToastRecord r = mToastQueue.get(i);
                             if (r.pkg.equals(pkg)) {
                                 count++;
                                 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
                                     Slog.e(TAG, "Package has already posted " + count
                                            + " toasts. Not showing more. Package=" + pkg);
                                     return;
                                 }
                             }
                        }
                    }

                    record = new ToastRecord(callingPid, pkg, callback, duration);
                    mToastQueue.add(record);
                    index = mToastQueue.size() - 1;
                    keepProcessAliveLocked(callingPid);
                }
                // If it's at index 0, it's the current toast.  It doesn't matter if it's
                // new or just been updated.  Call back and tell it to show itself.
                // If the callback fails, this will remove it from the list, so don't
                // assume that it's valid after this.
                if (index == 0) {
                    showNextToastLocked();
                }
            } finally {
                Binder.restoreCallingIdentity(callingId);
            }
        }

这其中的内容主要就是判断一下Toast在index中位置是不是0,如果是,就显示,不是,就刷新自己。下面还要看看showNextToastLocked();

private void showNextToastLocked() {
        ToastRecord record = mToastQueue.get(0);
        while (record != null) {
            if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
            try {
                //正主终于来了,当年存入ToastRecord的回调终于派上用场,这里调用的,就是TN的show方法。
                record.callback.show();
                //计算好时间,显示既定时间之后就删除,这个函数一会儿再说。
                scheduleTimeoutLocked(record);
                return;
            } catch (RemoteException e) {
                Slog.w(TAG, "Object died trying to show notification " + record.callback
                        + " in package " + record.pkg);
                // remove it from the list and let the process die
                int index = mToastQueue.indexOf(record);
                if (index >= 0) {
                    mToastQueue.remove(index);
                }
                keepProcessAliveLocked(record.pid);
                if (mToastQueue.size() > 0) {
                    record = mToastQueue.get(0);
                } else {
                    record = null;
                }
            }
        }
    }

刚才终于通过binder引用回调了TN的show方法,那么我们回过头去看:

TN调用WMS显示Toast

代码很长,但是总结一下其实就是在请求线程中执行handleShow()方法,利用WindowManager的addView()方法,在Window中添加toast。

@Override
public void show() {
    if (localLOGV) Log.v(TAG, "SHOW: " + this);
    mHandler.post(mShow);
}
final Runnable mShow = new Runnable() {
    @Override
    public void run() {
        handleShow();
    }
};
public void handleShow() {
            if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
                    + " mNextView=" + mNextView);
            if (mView != mNextView) {
                // remove the old view if necessary
                handleHide();
                mView = mNextView;
                Context context = mView.getContext().getApplicationContext();
                String packageName = mView.getContext().getOpPackageName();
                if (context == null) {
                    context = mView.getContext();
                }
                mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
                // We can resolve the Gravity here by using the Locale for getting
                // the layout direction
                final Configuration config = mView.getContext().getResources().getConfiguration();
                final int gravity = Gravity.getAbsoluteGravity(mGravity, config.getLayoutDirection());
                mParams.gravity = gravity;
                if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
                    mParams.horizontalWeight = 1.0f;
                }
                if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
                    mParams.verticalWeight = 1.0f;
                }
                mParams.x = mX;
                mParams.y = mY;
                mParams.verticalMargin = mVerticalMargin;
                mParams.horizontalMargin = mHorizontalMargin;
                mParams.packageName = packageName;
                if (mView.getParent() != null) {
                    if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
                    mWM.removeView(mView);
                }
                if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
                mWM.addView(mView, mParams);
                trySendAccessibilityEvent();
            }
        }

NMS回调TN

在NMS中又是怎么计时的呢,还记得刚才在回调TN的show()方法之后调用的方法吗?scheduleTimeoutLocked();

 private void scheduleTimeoutLocked(ToastRecord r)
    {
        mHandler.removeCallbacksAndMessages(r);
        Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
        long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY;
        mHandler.sendMessageDelayed(m, delay);
    }

很简单的一个函数,取需要显示的时间delay,然后发送一个延迟delay时间的消息,并且将ToastRecorder对象传递过去,这个消息是干嘛呢,继续往下看:

private final class WorkerHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case MESSAGE_TIMEOUT:
                    handleTimeout((ToastRecord)msg.obj);
                    break;
            }
        }
    }

收到消息后,执行handleTimeOut方法,并且将该ToastRecorder对象传递过去:

 private void handleTimeout(ToastRecord record)
    {
        if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
        synchronized (mToastQueue) {
            int index = indexOfToastLocked(record.pkg, record.callback);
            if (index >= 0) {
                cancelToastLocked(index);
            }
        }
    }

直接看最后一句,cancelToastLocked,在一个Toast显示的时候其他Toast不能显示,现在肯定是先取消这个锁,继续看:

private void cancelToastLocked(int index) {
        ToastRecord record = mToastQueue.get(index);
        try {
            //在这里调用了TN的hide()方法
            record.callback.hide();
        } catch (RemoteException e) {
            Slog.w(TAG, "Object died trying to hide notification " + record.callback
                    + " in package " + record.pkg);
            // don't worry about this, we're about to remove it from
            // the list anyway
        }
        mToastQueue.remove(index);
        keepProcessAliveLocked(record.pid);
        if (mToastQueue.size() > 0) {
            // Show the next one. If the callback fails, this will remove
            // it from the list, so don't assume that the list hasn't changed
            // after this point.
            showNextToastLocked();
        }
    }

在这个函数里面,首先是调用TN的hide()方法,隐藏显示完毕的Toast,然后判断还有没有需要显示的Toast,如果有的话,继续显示下一条。

TN调用WMS删除Toast

到这里已经差不多了,TN的hide被客户端调用,发生的情况和show()差不多,也是在请求线程中通过WindowManager,remove掉刚才添加的Toast。

@Override
        public void hide() {
            if (localLOGV) Log.v(TAG, "HIDE: " + this);
            mHandler.post(mHide);
        }
final Runnable mHide = new Runnable() {
            @Override
            public void run() {
                handleHide();
                // Don't do this in handleHide() because it is also invoked by handleShow()
                mNextView = null;
            }
        };
 public void handleHide() {
            if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView);
            if (mView != null) {
                // note: checking parent() just to make sure the view has
                // been added...  i have seen cases where we get here when
                // the view isn't yet added, so let's try not to crash.
                if (mView.getParent() != null) {
                    if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
                    mWM.removeView(mView);
                }

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

推荐阅读更多精彩内容