Android development-Toast enabling and disabling

Source: Internet
Author: User

Android development-Toast enabling and disabling

Before developing this program, explain why the Toast information prompt box will automatically disappear after a certain period of time? Because there is a Toast queue in the Android system, the system extracts a Toast from the queue in sequence and displays it. Close it after the specified time is displayed. Can I keep the Toast prompt box displayed? This requirement is a bit difficult for Toast, because Toast itself does not provide this function. So how can we keep Toast displayed and disable it under our control? Code is dead, and people are active. Let's talk about the code below:

Let's take a look at the source code of the Toast show method:

public void show() {        if (mNextView == null) {            throw new RuntimeException("setView must have 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 responsible for displaying and disabling the information prompt box. It just adds Toast to the system's Toast queue, then, the system displays and closes the Toast information prompt Box Based on the Toast queue. Now we can make a bold inference. Since the Toast show method places Toast in the system's Toast queue, we will not use the show method, we can control Toast display and close by ourselves.

View the source code of the Toast class to find a TN class, which is an embedded class of Toast. There is a show method in the TN class. After obtaining the Toast object from the Toast queue, the show method of the TN object shows that Toast is disabled by using the TN. hide method. If we can get the TN object, we can control Toast display and close. However, TN is declared as private and cannot be accessed from outside. However, the Toast class has an mTN object. Although it is not public, we can use the Java reflection technology to access this object. MTN is initialized when the Toast object is created. Therefore, as long as the mTN object is obtained, the TN object is obtained. The following code displays a prompt box for the Toast information that will never be closed.

Toast toast = Toast. makeText (context, msg, Toast. LENGTH_SHORT); // sets the Toast display position toast. setGravity (Gravity. TOP | Gravity. CENTER_HORIZONTAL, 0, 0); try {// obtain the mTN object Field = toast from the toast object through the reflection technique. getClass (). getDeclaredField ("mTN"); field. setAccessible (true); obj = field. get (toast); // obtain the show Method method = obj from the TN object. getClass (). getDeclaredMethod ("show", null); // call the show method of the TN object to display the Toast information prompt box method. invoke (obj, null);} catch (Exception e ){}

The above code first obtains the mTN object through the previously created Toast object, and then obtains the show method of the TN object by using the reflection technology.

The method for disabling Toast is similar to that for displaying Toast. You only need to obtain the hide method.

Method method = obj.getClass().getDeclaredMethod("hide", null);method.invoke(obj, null);

Program running:


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.