Android Toast有TextToastRecord和CustomToastRecord两种方式进行显示,最主要的差别在service.enqueueTextToast(pkg, mToken, mText, mDuration, displayId, callback); TextToastRecord的callback等于null
@MainActivity.java
Toast.makeText(MainActivity.this, "xww make toast", Toast.LENGTH_SHORT)
@frameworks/base/core/java/android/widget/Toast.java
makeText(context, null, text, duration);
Toast result = new Toast(context, looper);
mContext = context; // Toast的context
mTN = new TN(context, context.getPackageName(), mToken, mCallbacks, looper);
@frameworks/base/core/java/android/widget/ToastPresenter.java
mPresenter = new ToastPresenter(context, accessibilityManager, getService(), packageName);
mContext = context; //ToastPresenter的context
@frameworks/base/core/java/android/app/ContextImpl.java
mWindowManager = context.getSystemService(WindowManager.class);
SystemServiceRegistry.getSystemServiceName(serviceClass);
final String serviceName = SYSTEM_SERVICE_NAMES.get(serviceClass);
return serviceName;
View v = ToastPresenter.getTextToastView(context, text);
View view = LayoutInflater.from(context).inflate(TEXT_TOAST_LAYOUT, null);
TextView textView = view.findViewById(com.android.internal.R.id.message);
textView.setText(text);
return view;
result.mNextView = v; // Toast的context
.show())
INotificationManager service = getService();
sService = INotificationManager.Stub.asInterface(ServiceManager.getService(Context.NOTIFICATION_SERVICE));
TN tn = mTN;
tn.mNextView = mNextView; // 设置TN的mNextView
final int displayId = mContext.getDisplayId();
@frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
service.enqueueTextToast(pkg, mToken, mText, mDuration, displayId, callback); //TextToastRecord和CustomToastRecord主要是这里的差异, callback等于null
enqueueToast(pkg, token, text, null, duration, displayId, callback);
@frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
mWindowManagerInternal.addWindowToken(windowToken, TYPE_TOAST, displayId,null
record = getToastRecord(callingUid, callingPid, pkg, isSystemToast, token, text, callback, duration, windowToken, displayId, textCallback); // callback等于null
f (callback == null) { return new TextToastRecord(this, mStatusBar, uid, pid, packageName, isSystemToast, token, text, duration, windowToken, displayId, textCallback);
final DisplayContent dc = getDisplayContentOrCreate(displayId, null /* token */);
new WindowToken.Builder(this, binder, type).setDisplayContent(dc) .setPersistOnEmpty(true).setOwnerCanManageAppTokens(true).setOptions(options).build();
mToastQueue.add(record);
showNextToastLocked(false);
ToastRecord record = mToastQueue.get(0);
tryShowToast(record, rateLimitingEnabled, isWithinQuota, isPackageInForeground))
@frameworks/base/services/core/java/com/android/server/notification/toast/TextToastRecord.java
record.show();
@frameworks/base/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
mStatusBar.showToast(uid, pkg, token, text, windowToken, getDuration(), mCallback); //进入到com.android.systemui
@frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
mBar.showToast(uid, packageName, token, text, windowToken, duration, callback);
mHandler.obtainMessage(MSG_SHOW_TOAST, args).sendToTarget();
@frameworks/base/packages/SystemUI/src/com/android/systemui/toast/ToastUI.java
case MSG_SHOW_TOAST: { callbacks.showToast(uid, packageName, token, text, windowToken, duration, callback);
mPresenter = new ToastPresenter(context, mIAccessibilityManager, mNotificationManager, packageName);
mToastLogger.logOnShowToast(uid, packageName, text.toString(), token.toString());
mPresenter.show(mToast.getView(), token, windowToken, duration, mToast.getGravity(),mToast.getXOffset(), mToast.getYOffset(), mToast.getHorizontalMargin(), mToast.getVerticalMargin(), mCallback, mToast.hasCustomAnimation());