A variety of applications for Android-developed toast

Source: Internet
Author: User

Toast the basic function is to pop a weak hint, this is very simple I will not say, say toast some other role.

When we came to the company, The company's product version 1.0 has been released, but 1.0 is a must be connected to the use of products, in 2.0 want to let users offline can also be used, but offline words in the title bar below a hint, indicating that the current offline state, this is also a simple thing, but because the early product is more personal development, resulting in the title bar unexpectedly Using four or five headings, the public activity has 5, so the workload is very large, and then after thinking I decided to solve this problem one time in the subclass of application, the control used is toast.

Under normal circumstances, the longest time the toast is displayed is Toast.length, This value is 3.5 seconds, does not meet the requirements, I want the entire app to run without a network when it can be displayed, and when the program exits or into the background to run the end, then my solution is to use a thread, and then use the while (true) loop, Determines whether a toast is displayed in a loop based on conditions. In addition to the layout and display location of the toast I also handle by customizing.

Well, nonsense not much to say, on the code:

 Public  class myapplication extends application {    PrivateBroadcastreceiver receiver;PrivateToast toast =NULL;Private BooleanIsnotnet =false;Private BooleanIsquit =false;@Override     Public void onCreate() {Super. OnCreate (); Inittoast ();//acitivty Call the life-cycle method when the method is called back here         This. Registeractivitylifecyclecallbacks (NewActivitylifecyclecallbacks () {@Override             Public void onactivitystopped(Activity activity) {isquit = Isapplicationbroughttobackground (MyApplication. This);            Toast.cancel (); }@Override             Public void onactivitystarted(Activity activity) {isquit = Isapplicationbroughttobackground (MyApplication. This);            Showtoast (); }@Override             Public void onactivitysaveinstancestate(Activity activity, Bundle outstate) {            }@Override             Public void onactivityresumed(Activity activity) {            }@Override             Public void onactivitypaused(Activity activity) {            }@Override             Public void onactivitydestroyed(Activity activity) {isquit = Isapplicationbroughttobackground (MyApplication. This);            Toast.cancel (); }@Override             Public void onactivitycreated(Activity activity, Bundle savedinstancestate)    {            }        }); }Private void Inittoast() {receiver =NewBroadcastreceiver () {@Override             Public void OnReceive(context context, Intent Intent) {toast = Toast.maketext (MyApplication. This,"a net", Toast.length_short);//Get a custom toast layoutView view = Layoutinflater.from (MyApplication. This). Inflate (R.layout.layout_toast,NULL);//Set toast position at top, x-axis offset to 0,y axis offset toToast.setgravity (Gravity.top,0, $);                Toast.setview (view); Connectivitymanager Connectivitymanager = (connectivitymanager) context. Getsystemservice (context.c Onnectivity_service);if(Connectivitymanager! =NULL) {Networkinfo Networkinfo = Connectivitymanager. Getactivenetworkinfo ();if(Networkinfo = =NULL) {//Off-gridIsnotnet =true;                    Showtoast (); }Else{//Have a networkIsnotnet =false; }                }            }        };//Registered broadcastIntentfilter Intentfilter =NewIntentfilter (); Intentfilter.addaction ("Android.net.conn.CONNECTIVITY_CHANGE");    Registerreceiver (receiver, intentfilter); }Private void Showtoast() {NewThread (NewRunnable () {@Override             Public void Run() {Try{//for (int i = 0; i <; i++) {                     while(true){//No NET and program does not exit                        if(Isnotnet &&!isquit) {//Exectoast (toast);Toast.show (); Thread.Sleep ( +); }                    }                }Catch(Interruptedexception e)                {E.printstacktrace ();    }}). Start (); }Private void Exectoast(FinalToast t) {//Timer timer = new timer ();//Timer.schedule (new TimerTask () {////@Override//public void Run () {////tune the main thread method, otherwise it may not show up. //Inittoast (t);//          }////private void Inittoast (Toast t) {//T.show ();//          }////}, 0);T.show (); }/** * Determine if the program is running in the background * @param context * @return  * *     Public Boolean Isapplicationbroughttobackground(FinalContext context) {Activitymanager am = (activitymanager) context. Getsystemservice (context.activity        _service); list<runningtaskinfo> tasks = Am.getrunningtasks (1);if(!tasks.isempty ()) {ComponentName topactivity = Tasks.get (0). topactivity;if(!topactivity.getpackagename (). Equals (Context.getpackagename ())) {return true; }        }return false; }}

For the usage of application, you can view the rewritten application class for Android development, which requires the following permissions:

    <!-- 判断网络状态 -->    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <!-- 判断程序是否在后台运行 -->    <uses-permission android:name="android.permission.GET_TASKS" />

Finally, toast can also be used for caller ID display.

Download the project Http://pan.baidu.com/s/1qWmRlt6

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Multiple apps for Android-developed toast

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.