Checking network status on button click

2019-08-01 16:14发布

问题:

In my android app I have a button that when clicked, opens another class that makes a network call.

I am trying to check for a data connection when the button is clicked and if there isn't a connection I want to display a toast message.

Here is my onclick method as it stands:-

public void GoToZone(View v)
{



    if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
          StrictMode.setThreadPolicy(policy);
          Intent myIntent = new Intent(MainActivity.this, CustomizedListViewStudentZone.class);
          startActivityForResult(myIntent, 0);
        } 


    else {            
        Context context = getApplicationContext();
        CharSequence text = "There seems to be a connection issue";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }



 }

How would I be able to check for a connection when the button is pressed and if there is a connection then proceed with the above and if there isn't then display a message saying "No Connection"

I am really struggling with implementing the check.

Thanks

回答1:

someMethod(){
    if (isNetworkAvailable()) { 
        Toast.makeText(this, "woohoo!", Toast.LENTH_SHORT).show(); 
    } else {
        Toast.makeText.(this, "booo..", Toast.LENGTH_SHORT).show(); 
    }
}


private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
              = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Source



回答2:

public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) {
                  Log.d("Nzm", "info:"+info[i]);
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }
              }

      }
      return false;
}


回答3:

Simply use it.

Call this code from anywhere it will return you the current state of the Network.

public static boolean isInternetAvailable(Context c)
    {
         ConnectivityManager connectivityManager 
         = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
         return activeNetworkInfo != null && activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED;
    }

return true if it is connected, false otherwise. Take any desired action on its output.

Note: Its a static method. So remove static keyword if you just want to call it from within an Android Activity class.