Preface
I believe every Android developer has used itToast
, everyone knows that it is a pop-up message. Similar to the one in jsalert
, in C#MesageBox
. Of course there is alsodialog
,dialog
It has a focus and can interact with users. andtoast
There is no focus, the time will disappear automatically, and the user's interaction cannot be responded to. Let's share with you the AndroidToast
Optimization method for prompt box.
Let's look at the source code first:
public class Toast { public static final int LENGTH_SHORT = 0; public static final int LENGTH_LONG = 1; /** * Construct an empty toast. You must call setView() on the line before you can redirect show() * @param context parameter, application or activity can be */ public Toast(Context context) { ... //Get the toast_y_offset constant value built in the system = ().getDimensionPixelSize( .toast_y_offset); = ().getInteger( .config_toastDefaultGravity); } /** * Display view for specified duration */ public void show() { //If mNextView is empty, no view is set if (mNextView == null) { throw new RuntimeException("setView must have been called"); } //Get notification manager through system services. It seems this is using system notification? INotificationManager service = getService(); String pkg = (); TN tn = mTN; = mNextView; try { (pkg, tn, mDuration); } catch (RemoteException e) { // Empty } } /** * Close a toast that is being displayed, or cancel an toast that is not displayed. * Usually you don't have to call it, it will disappear automatically after the appropriate time. */ public void cancel() { (); try { getService().cancelToast((), mTN); } catch (RemoteException e) { // Empty } } /** * Set the view content displayed by toast, not just a black interface, you can decide what to display * @see #getView */ public void setView(View view) { mNextView = view; } /** * The setting time can only be the following two constant values, which is useless * @see #LENGTH_SHORT 2000ms * @see #LENGTH_LONG 3500ms */ public void setDuration(@Duration int duration) { mDuration = duration; } /** * Set the outer spacing of the view, needless to say. * * @param horizontalMargin The horizontal margin, in percentage of the * container width, between the container's edges and the * notification * @param verticalMargin The vertical margin, in percentage of the * container height, between the container's edges and the * notification */ public void setMargin(float horizontalMargin, float verticalMargin) { = horizontalMargin; = verticalMargin; } /** * Everyone knows the position of notification in the screen. There are both upper, middle, lower, left, right and so on. * @see * @see #getGravity */ public void setGravity(int gravity, int xOffset, int yOffset) { = gravity; = xOffset; = yOffset; } /** * Construct a standard toast object containing only one TextView * * @param context is usually an application or activity object * @param text The text used for display, can be formatted text. * @param duration Display duration. LENGTH_SHORT or LENGTH_LONG * */ public static Toast makeText(Context context, CharSequence text, @Duration int duration) { Toast result = new Toast(context); LayoutInflater inflate = (LayoutInflater) (Context.LAYOUT_INFLATER_SERVICE); //Includes a default TextView, the layout position of this textview is .transient_notification,You can check the content View v = (.transient_notification, null); TextView tv = (TextView)(); (text); = v; = duration; return result; } /** * Update the text content displayed by the toast object created through the makeText() method * @param s New text content to be displayed. */ public void setText(CharSequence s) { if (mNextView == null) { throw new RuntimeException("This Toast was not created with ()"); } /** * It seems that the only one in the .transient_notification layout * The id of the TextView tag is. Get this textview and set new text content */ TextView tv = (TextView) (); if (tv == null) { throw new RuntimeException("This Toast was not created with ()"); } (s); } static private INotificationManager getService() { if (sService != null) { return sService; } //Get remote notification service sService = (("notification")); return sService; } //TN is a subclass of transient notifications, which contains two task objects to show and hide. private static class TN extends { final Runnable mShow = new Runnable() { @Override public void run() { handleShow(); } }; 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; } }; //Handler appears final Handler mHandler = new Handler(); /** * Scheduling handleShow task into execution thread */ @Override public void show() { if (localLOGV) (TAG, "SHOW: " + this); //Handler sends an asynchronous task (mShow); } /** * Same as above */ @Override public void hide() { if (localLOGV) (TAG, "HIDE: " + this); (mHide); } //... } }
Through the above source code interpretation, we learned that there is remote notification.handler
If you don’t say much about asynchronous tasks, please read it yourself.
The point is how toast is used:
1. Just call the makeText static method directly. The return is the Toast object. Finally, don’t forget to call the show method to display:
(context, text, duration).show();
or
Toast toast = (context, text, duration); pre name="code" class="html"> (view); (s); (gravity, xOffset, yOffset); (duration); ();
2. You can also create it in new way, don’t forget to setView() method:
Toast toast = new Toast(); (view); (s); (gravity, xOffset, yOffset); (duration); ();
None of the above is worth mentioning, it is very simple.
During the development process, there is such a requirement: in the project, we are lazy and want to make a series oftoast
The values of multiple variables or other tasks can be visually visible when operating the phone. The problem is that no matter how fast our operation is, thesetoast
The content is displayed one by one, and there is no way to fast forward. Even if we finish playing and exit the app, it is still playing. what to do? Is there any way to lettoast
The content is synchronized with our operations and respond quickly?
public class T { private static Toast toast; public static void show(Context context, String msg) { if (toast == null) { toast = (context, msg, Toast.LENGTH_SHORT); } else { (msg); } (); } }
Singleton pattern, created every timetoast
All call this classshow
method,Toast
The life cycle fromshow
Start, until you disappear orcancel
until. If it is being displayed, modify the displayed content. If you hold a reference to this object, of course you can modify the displayed content. If you alwaysmakeText
ornew
onetoast
Object, that is, every timehandler
Send asynchronous tasks and call the remote notification service to display notifications, of course, waiting in line to display.
Conclusion
The above is all the content of the Toast prompt box optimization in Android. I hope it will be helpful to everyone's development of Android. If you have any questions, you can leave a message to communicate.