Before you develop this program, explain why the toast information box disappears automatically after a certain amount of time is displayed. Because there is a toast queue in the Android system, a toast is taken out of the queue and displayed in turn. After the specified time is displayed, close it. Can you make the toast message prompt appear? This requirement is a bit of a imposition for toasts because the toast itself does not provide this functionality. So how do you get toast to show up and shut Down under our control? The code is dead, the man is alive. Here is the code to speak:
Look at the source code of the Toast Show method:
public void Show () { if (Mnextview = = null) { throw new RuntimeException ("Setview must has been called") ;} Inotificationmanager service = GetService (); String pkg = Mcontext.getpackagename (); TN tn = MTN; Tn.mnextview = Mnextview; try { Service.enqueuetoast (pkg, TN, mduration); } catch (RemoteException e) { //Empty } }
In the above code, the toast has already told us that it is not responsible for the display and closure of the informational cue box, it simply joins the toast to the system's toast queue, and the toast information prompt is displayed and closed by the system based on the toast queue. Now we can make a bold inference, since the toast's Show method is to put toast in the system's toast queue, then we don't use the Show method, we control the display and closure of the toast ourselves.
View Toast class Source You can find a TN class, which is an inline class for toast. There is a show method in the tn class that, after the toast object is obtained from the toast queue, displays the toast using the Show method of the TN object and then uses the tn.hide method to close the toast. If we can get the TN object Then we can control the display and shutdown of the toast. However, TN is declared as private and cannot be accessed externally. However, there is a MTN object in the Toast class, although it is not my public but we can access the object through Java reflection technology. MTN Initializes the toast object when it is created. Therefore, the TN object is obtained as long as the MTN object is obtained. The following code shows a toast information prompt that will never be closed.
Toast Toast = Toast.maketext (context, MSG, toast.length_short); Set where toast is displayed toast.setgravity (gravity.top | Gravity.center_horizontal, 0, 0); Try { ///Through reflection technology, get the MTN object from the Toast object Field field = Toast.getclass (). Getdeclaredfield ("MTN"); Field.setaccessible (true); obj = Field.get (toast); Get the Show method from the TN object methods = Obj.getclass (). Getdeclaredmethod ("show", null); Call the Show method of the TN object to display the Toast information hint box method.invoke (obj, null); } catch (Exception e) { }
The above code first obtains the MTN object through a pre-created Toast object, and then obtains the show method of the TN object using the reflection technique.
The method of closing a toast is similar to displaying a toast, just get the hide method.
method = Obj.getclass (). Getdeclaredmethod ("Hide", null); Method.invoke (obj, null);
program run: