Android Development tips for never-closed toast information box (long time display rather than system shutdown) _android

Source: Internet
Author: User
Tags reflection
The toast message balloon closes automatically after a certain amount of time is displayed because there is a toast queue in the system. The system takes (out of the queue) a toast from the queue in turn and displays it. After displaying a period of time, close again, and then display the next toast message balloon. Until all toast in the toast queue are displayed. There are times when you need this toast message box to display for a long time, until you need to close it to control by code, rather than having the system automatically turn off the toast information balloon. But this requirement is a bit excessive for toast itself, because the toast class does not provide this functionality. Nevertheless, there are more methods than problems. This functionality can be achieved through some special processing, and it is not complicated.

As you can see from the 7.3.1 section, the Toast Information prompt box needs to call the Toast.show method to display it. Let's look at the source code for the Show method.
Copy Code code as follows:

Publicvoidshow () {
if (mnextview==null) {
Thrownewruntimeexception ("setviewmusthavebeencalled");
}
Inotificationmanagerservice=getservice ();
Stringpkg=mcontext.getpackagename ();
Tntn=mtn;
try{
Add the current toast to the toast queue
Service.enqueuetoast (pkg,tn,mduration);
}catch (Remoteexceptione) {
Empty
}
}

The code for the Show method is not complex and it is easy to find the following code.
Copy Code code as follows:

Service.enqueuetoast (pkg,tn,mduration);

It is easy to infer from the above code that its function is to add the current toast to the system's toast queue. See here, you readers should think. Although the surface function of the show method is to display the toast information balloon, its actual function is to add toast to the queue and the system to display the toast message balloon according to the toast queue. Then we can boldly make a preliminary plan after thinking further. Since the system's toast queue can display a toast information balloon, why can't we show it ourselves? This is not a way to control the toast information box display and closed it! Of course, this can no longer call the Show method to display the toast message balloon (because the shows method joins toast into the queue, so we can't control the toast).

Now that the initial programme has been drafted, it will be implemented. First find out if there are any other show methods in the Toast category. The results found a TN class, which is an inline class of toast. There is a show method in the TN class. TN is a subclass of Itransientnotification.stub. A preliminary inference from the Show method in the Itransientnotification and TN classes (since transient's Chinese meaning is "ephemeral") is that the Toast object is obtained from the toast queue, and the display method of the TN object shows Toast, Then use the Tn.hide method to close the toast. First of all, this is just hypothetical, we do not know whether this is feasible! Of course, this is also the general method of scientific research, first inferred or assumed, and then prove the inference or hypothesis.

The key step now is to get the TN object. Unfortunately, TN is declared private and inaccessible outside. But don't worry. There is a MTN variable in the Toast class. Although not a public variable, the variable can still be accessed through reflection technology. The MTN variable is initialized when the Toast object is created. Therefore, as long as the MTN variable is obtained, the TN object is obtained. The following code shows a toast message balloon that will never be automatically closed.
Copy Code code as follows:

First create a Toast object
Toasttoast=toast.maketext (This, "Toast never Disappears", toast.length_short);
Set the location of the toast Information balloon display (centered horizontally at the top of the screen)
Toast.setgravity (gravity.top| gravity.center_horizontal,0,0);
Try
{
Get the MTN variable from the Toast object
Fieldfield=toast.getclass (). Getdeclaredfield ("MTN");
Field.setaccessible (TRUE);
Objectobj=field.get (toast);
The TN object gets the Show method
Methodmethod=obj.getclass (). Getdeclaredmethod ("show", null);
Call the Show method to display the toast information balloon
Method.invoke (Obj,null);
}
catch (Exceptione)
{
}

In the code above, try{...} catch (...) {...} The code in the statement is critical. The MTN variable is obtained by first using the Toast object that was created beforehand. Then the Show method of TN object is obtained by using the technique of reflection.
The way to turn off toast and display toast is similar, just need to get the Hide method, the code is as follows:
Copy Code code as follows:

Try
{
You need to turn the obj variable in the preceding code into a class variable. So you can access it in multiple places.
Methodmethod=obj.getclass (). Getdeclaredmethod ("Hide", null);
Method.invoke (Obj,null);
}
catch (Exceptione)
{
}

The above code has perfectly implemented the ability to display and close the toast information balloon through code control. But if you want to achieve perfection, You can find a file called Itransientnotification.aidl in the ANDROIDSDK source code (which is a Aidl service definition file that will be described in detail later), and build a Android.app package in the SRC directory of the Android project to put this file in this package In Then ADT automatically generates a Android.app package in the Gen directory with a Itransientnotification.java file in the package. Because the ANDROIDSDK itransientnotification interface belongs to internal resources, the external program cannot access it, so You can only convert the MTN variable obtained from the Toast object to the Itransientnotification object you just generated. This does not require the show and hide methods to be obtained by the reflection technique. The code for the improved display and shutdown toast Information balloon is as follows:
Copy Code code as follows:

itransientnotificationnotification= (itransientnotification) field.get (toast);
Display Toast Information balloon
Notification.show ();
Turn off the Toast Info prompt box
Notification.hide ();
Related Article

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.