Android never turns off toast

Source: Internet
Author: User

The toast information prompt is automatically closed after a certain amount of time, because there is a toast queue in the system, and sometimes this toast information prompt is required to be displayed for a long time, until it is controlled by code when it needs to be closed, rather than having the system automatically close the Toast Information prompt box

The toast information prompt closes automatically after a certain amount of time 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 a period of time is displayed, close again, and then display the next Toast Information prompt box. Until all toasts in the toast queue are displayed. Then there are times when you need this toast info prompt to be displayed for a long time until it needs to be closed by the code, rather than having the system automatically close the Toast Information prompt box. However, this requirement is too much for toast itself, because the toast class does not provide this functionality. Nevertheless, the method is always more than the problem. This functionality can be achieved through some special processing, and it is not complicated.

The TOAST information hint box needs to be called by the Toast.show method to display. Here's a look at the source code for the Show method.

The Code of the Show method is not complex and can easily be found in the following code.

Service.enqueuetoast (pkg,tn,mduration);

From the code above it is easy to infer that its function is to add the current toast to the system's toast queue. See here, readers should think. Although the surface feature of the show method is to display a toast information prompt, its actual function is to join the toast to the queue and the system to display the toast information prompt based on the toast queue. Then we can make a tentative plan boldly by thinking further. Now that the system's toast queue can display a toast information prompt, why can't we show it ourselves? This is not the message that you can control the toast of the display and close the box! Of course, this can no longer call the Show method to display the Toast information prompt (because the Show method joins the toast to the queue so we can't control the toast).

Now that the initial programme has been drafted, it will be implemented. Find out the Toast class first and there are no other show methods. The result is 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. Initial inference from the Show method in the Itransientnotification and TN classes (because transient's Chinese meaning is "ephemeral") when the system is getting the toast object from the toast queue, the toast is displayed using the Show method of the TN object, Then use the Tn.hide method to close the toast. First of all, it's just a hypothesis, we don't know if it's possible! Of course, this is also a general method of scientific research, first inferred or assumed, and then proved inferred or assumed.

The key step now is to get the TN object. It is regrettable that TN is declared as private type and cannot be accessed externally. But don't worry. There is a MTN variable in the Toast class. Although it is 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, the TN object is obtained as long as the MTN variable is obtained. The following code shows a toast information prompt that is never automatically closed.

  

Try{in the code above ...} catch (...) {...} The code in the statement is key. The MTN variable is obtained by first using a pre-created Toast object. Then the Show method of TN object is obtained by using reflection technique.
The method of closing toast and displaying toast is similar, just need to get the Hide method, the code is as follows:

Try//methodmethod=obj.getclass (). Getdeclaredmethod ("hide  ",null); Method.invoke (obj,nullcatch

The code above has been perfectly implemented to control the display and shutdown of toast information prompts through code. But if you want to achieve perfection, You can find a file called Itransientnotification.aidl in the ANDROIDSDK source code (the file is a Aidl service definition file, which will be described in detail later) and a Android.app package in the SRC directory of the Android project to put this file into this package In ADT then automatically generates a Android.app package in the Gen directory, with a Itransientnotification.java file in the package. Because the Itransientnotification interface that ANDROIDSDK comes with is internal, 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 makes it unnecessary to have the show and hide methods available for reflection technology. The code for the improved display and closing of the Toast Info prompt box is as follows:

itransientnotificationnotification= (itransientnotification) field. Get ////
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.