Note: The following content is based on Android API Version 27 (Android 8.1) Linux Kernel 3.18.0
Toast show and disappear
Toast to create and display is occurred in the process of App, and Toast when displayed when disappear is controlled by NotificationManagerService.
A Toast has the following process from creation, display and disappearance:
- through
makeText
To create aToast
Object, createToast
The object is basically createdToast
theView
. - call
Toast
The object’sshow
Method,show
Method calledNotificationManagerService
theenqueueToast
willToast
One of the internal implementationsITransientNotification
calledTN
theBinder
The local object is passedNotificationManagerService
“And passed it onToast
The duration of. NotificationManagerService
To run onsystem_server
Process, responsible for managing all of the systemToast
.NotifactionManagerService
Process one by one in the queueToast
forToast
To create atoken
(new Binder()
) and add toWMS
(calladdWindowToken
),WMS
One will be created for itWindowToken
And record thetokenMap
.NotifactionManagerService
The callbackToast
theTN
theshow
Methods,token
Passed to the App processToast
Object.- The process of App
Toast
Object throughHandler
Switch to main thread executionWindowManager(WindowManagerImpl)
theaddView
willToast
theView
Added to theWMS
To display itself, including the logic of view rule PL. - When the display time arrives
NotificationManagerService
The callbackTN
thehide
Method is then called by the App processWMS
removeToast
.
conclusion
1, NotificationManagerService unified management system in all display and disappear, Toast Toast the real display and disappear operation performed by the process of App Toast object, This is in line with the idea of ActivityManagerService for activities.
2. The window type of Toast is TYPE_TOAST. TYPE_TOAST is a system window, and Toast has its own token and is not controlled by the Activity.
3. Unlike Activity/Dialog, Toast adds its own View directly to WindowManagerImpl without creating a PhoneWindow or DecorView. Toast View events (if any) are sent directly to the Toast View Tree.