接下来看看service.enqueueToast(pkg, tn, mDuration)这段代码,相信有的小伙伴会质疑,这段代码报红色,如何查看呢?
![image image]()
- 于是,我直接在studio中全局搜索NotificationManagerService,终于给找到了,如下所示:
![image image]()
- 下面就到重点呢……注意:record是将Toast封装成ToastRecord对象,放入mToastQueue中。通过下面代码可以得知:通过isSystemToast判断是否为系统Toast。如果当前Toast所属的进程的包名为“android”,则为系统Toast。如果是系统Toast一定可以进入到系统Toast队列中,不会被黑名单阻止。
synchronized (mToastQueue) {
int callingPid = Binder.getCallingPid();
long callingId = Binder.clearCallingIdentity();
try {
ToastRecord record;
int index;
//判断是否是系统级别的吐司
if (!isSystemToast) {
index = indexOfToastPackageLocked(pkg);
} else {
index = indexOfToastLocked(pkg, callback);
}
if (index >= 0) {
record = mToastQueue.get(index);
record.update(duration);
record.update(callback);
} else {
//创建一个Binder类型的token对象
Binder token = new Binder();
//生成一个Toast窗口,并且传递token等参数
mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY);
record = new ToastRecord(callingPid, pkg, callback, duration, token);
//添加到吐司队列之中
mToastQueue.add(record);
//对当前索引重新进行赋值
index = mToastQueue.size() - 1;
}
//将当前Toast所在的进程设置为前台进程
keepProcessAliveIfNeededLocked(callingPid);
if (index == 0) {
//如果index为0,说明当前入队的Toast在队头,需要调用showNextToastLocked方法直接显示
showNextToastLocked();
}
} finally {
Binder.restoreCallingIdentity(callingId);
}
}