Multiple applications of Toast in android development and toast in android Development

Source: Internet
Author: User

Multiple applications of Toast in android development and toast in android Development

The most basic function of Toast is to bring up a weak prompt. This is simple and I will not talk about it. Let's talk about some other functions of Toast.

When I came to the company, version 1.0 of the company's products had been released, but Version 1.0 is a product that must be connected to the Internet. In version 2.0, users can also use it offline, however, if it is offline, a prompt should be displayed below the title bar, indicating that it is currently offline. This is also a simple task. However, since the previous product was developed by many people, as a result, four or five titles were used in the title bar, and five common activities were used. In this way, the workload was very heavy. After thinking about this, I decided to solve this problem in the subclass of the Application, the used control is Toast.

Under normal circumstances, the maximum display time of Toast is Toast. LENGTH. The value is 3.5 seconds, which does not meet the requirements. I hope the whole App can be displayed when it is running without a network, and it ends when the program exits or is transferred to the background for running, so my solution is to use a thread and then use the while (true) loop to determine whether Toast is displayed based on the conditions in the loop. In addition, I also customize the Toast layout and display position.

Okay, let's talk about the Code:

Public class MyApplication extends Application {private BroadcastReceiver extends er; private Toast toast = null; private boolean isNotNet = false; private boolean isQuit = false; @ Override public void onCreate () {super. onCreate (); initToast (); // this method will be called back when acitivty calls the lifecycle method. registerActivityLifecycleCallbacks (new ActivityLifecycleCallbacks () {@ Override public void onActivityStopped (Activity acti Required) {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 vo Id 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 ER = new BroadcastReceiver () {@ Override public void onReceive (Context context, Inte Nt intent) {toast = Toast. makeText (MyApplication. this, "youwang", Toast. LENGTH_SHORT); // get the custom Toast Layout View = LayoutInflater. from (MyApplication. this ). inflate (R. layout. layout_toast, null); // you can specify the Toast position on the top, the offset on the X axis is 0, and the offset on the Y axis is 140 toast. setGravity (Gravity. TOP, 0,140); toast. setView (view); ConnectivityManager connectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVI CE); if (connectivityManager! = Null) {NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo (); if (networkInfo = null) {// disconnected isNotNet = true; showToast () ;}} else {// has a network isNotNet = false ;}}}}; // register broadcast IntentFilter intentFilter = new IntentFilter (); intentFilter. addAction ("android.net. conn. CONNECTIVITY_CHANGE "); registerReceiver (receiver, intentFilter);} private void showToast () {new Thread (new Runnable () {@ Override public void run () {try {// for (int I = 0; I <2000; I ++) {while (true) {// No network and the program has not exited if (isNotNet &&! IsQuit) {// execToast (toast); toast. show (); Thread. sleep (1000) ;}} catch (InterruptedException e) {e. printStackTrace ();}}}). start ();} private void execToast (final Toast t) {// Timer timer = new Timer (); // timer. schedule (new TimerTask () {//// @ Override // public void run () {/// call the main thread method. Otherwise, it may not be displayed. // InitToast (t); //} // private void initToast (Toast t) {// t. show (); //}, 0); t. show ();}/*** determine whether the program is transferred to the background for running * @ param context * @ return */public boolean isApplicationBroughtToBackground (final Context 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 the Application, you can view the override Application class of android development. The following permissions are required:

<! -- Determine the network status --> <uses-permission android: name = "android. permission. INTERNET "/> <uses-permission android: name =" android. permission. ACCESS_NETWORK_STATE "/> <! -- Determine whether the program runs in the background --> <uses-permission android: name = "android. permission. GET_TASKS"/>

Finally, Toast can also be used for incoming call display.

Download http://pan.baidu.com/s/1qWmRlt6 for this project

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. I am very grateful if you have any mistakes.

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.