Android development controls the opening and closing of toasts

Source: Internet
Author: User

Before you develop this program, explain why the toast message prompt disappears 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. Wouldn't it be possible to have the Toast info box show up all the time? This requirement is a bit of a imposition for toasts, because the toast itself does not provide this functionality. So how does a toast keep showing up and shutting down under our control? The code is dead, the man is alive. The following is called code to speak:

Take a 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, toast has already told us that it is not in itself responsible for the display and closure of the informational cue box, it simply adds 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 judgment, since the toast's Show method is to put toast in the system's toast queue, then we do not use the Show method, we control the display and closure of the toast ourselves.

View Toast class source code to find a TN class, which is an inline class of 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. Let's say we can get the TN object So we can control the display and closure of the toast. But TN is declared private and cannot be interviewed externally. There is only one MTN object in the Toast class, although it is not my public but we are able to access the object through Java reflection technology. MTN Initializes the toast object when it is created. Therefore, a TN object is obtained only if 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 way to close a toast is similar to displaying a toast, just need to get the hide method.

method = Obj.getclass (). Getdeclaredmethod ("Hide", null); Method.invoke (obj, null);

Program execution:


Android development controls the opening and closing of toasts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.