- Android Framework 之 Window / Win
- Android Framework 之 Window / Win
- Android Framework 之 Window / Win
- Android Framework 之 Window / Win
- Android Framework 之 Window / Win
- Android Framework 之 Window / Win
- Android Framework 之 Window / Win
- Android Window / WindowManager 小
- 日更挑战-Android Window、Activity、Dec
- Android UI-测量-布局-绘制-机制
Toast 的实现是基于 Window, 但是和 Activity/Dialog 创建 Window 的过程不一样,
它是依赖于 NotificationManagerService .
1. Toast.show()
直接看源码
//Toast.java
package android.widget;
public class Toast {
public Toast(@NonNull Context context, @Nullable Looper looper) {
.....
mTN = new TN(context, context.getPackageName(), mToken, //(1)
...
}
public void show() {
INotificationManager service = getService();//(2)
TN tn = mTN;
tn.mNextView = mNextView;
try {
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) {
if (mNextView != null) {
// It's a custom toast
service.enqueueToast(pkg, mToken, tn, mDuration, displayId);
} else {
// It's a text toast
ITransientNotificationCallback callback =
new CallbackBinder(mCallbacks, mHandler);
service.enqueueTextToast(pkg, mToken, mText, mDuration, displayId, callback);
}
} else {
service.enqueueToast(pkg, mToken, tn, mDuration, displayId); //(3)
}
} catch (RemoteException e) {
// Empty
}
}
}
>参考文献:
https://juejin.cn/post/7076274407416528909#heading-25
分析:
(1) 创建TN 内部类对象
(2) INotificationManager 是一个 AIDL 接口。 由 NotificationManagerService 实现
(3) 重点是调用 NotificationManagerService 的 enqueueToast 方法实现
enqueueToast 由 5个参数:
第一个表示当前应用的包名,第二个 token,第三个 tn 表示远程回调,也是一个 IPC 的过程,第四个 时长,第五个是显示的 id
其中 TN 为内部类
private static class TN extends ITransientNotification.Stub {
...
WindowManager mWM;
public void show(IBinder windowToken) {
....
mHandler.obtainMessage(SHOW, windowToken).sendToTarget();
}
}
未完待续...
网友评论