Checking network status on button click

北战南征 提交于 2019-12-11 11:16:00

问题


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.



来源:https://stackoverflow.com/questions/18109573/checking-network-status-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!