Hide a ProgressDialog over Firebase on a device without internet

后端 未结 1 341
别那么骄傲
别那么骄傲 2021-01-15 21:42

I have this scenario:

  • Show a progress dialog.
  • Call to Firebase.
  • When the data is got Hide the progress dialog

Works perfectly

相关标签:
1条回答
  • 2021-01-15 22:23

    You can check internet connection of device by using isConnected method of this class. Then you dismiss progress dialog when there is not internet connection

    public class InternetConnectionDetector
    {
       private Context context;
    
       public InternetConnectionDetector(Context context)
       {
           this.context = context;
       }
    
       public boolean isConnected()
       {
           ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
           //There are changes after apk lollipop while detecting internet connection:
           //if user's os newer then lollipop:
           if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
           {
               Network[] networks = connectivityManager.getAllNetworks();
               NetworkInfo networkInfo;
    
               for(Network mNetwork : networks)
               {
                   networkInfo = connectivityManager.getNetworkInfo(mNetwork);
    
                   if(networkInfo.getState().equals(NetworkInfo.State.CONNECTED))
                   {
                       return true;
                   }
               }
           }
    
           //if user's os older then lollipop:
           else
           {
               //getAllNetworkInfo method work for only before API 19:
               NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
    
               if(info != null)
               {
                   for(NetworkInfo anInfo : info)
                   {
                       if(anInfo.getState() == NetworkInfo.State.CONNECTED)
                       {
                           return true;
                       }
                   }
               }
           }
    
           return false;
       }
    }
    
    0 讨论(0)
提交回复
热议问题