how to detect internet connection in android studio

自作多情 提交于 2019-12-12 03:34:15

问题


I have following code to detect if there is internet connection available or not. But if I have no internet connection only the data connection is "ON" it still works. what I should do?

   ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);


    NetworkInfo ninfo = cManager.getActiveNetworkInfo();


    if(ninfo!=null && ninfo.isConnected())

    {
      Toast.makeText(this, "Available",Toast.LENGTH_LONG).show();
    }

    else
    {
      Toast.makeText(this, "Not Available",Toast.LENGTH_LONG).show();
    }

回答1:


Use this NetworkUtils class:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtils {

  private static int TYPE_WIFI = 1;
  private static int TYPE_MOBILE = 2;
  private static int TYPE_NOT_CONNECTED = 0;

  public static int getConnectivityStatus(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null) {
      if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI
          && networkInfo.getState() == NetworkInfo.State.CONNECTED) {

        return TYPE_WIFI;

      } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE
          && networkInfo.getState() == NetworkInfo.State.CONNECTED) {
        return TYPE_MOBILE;
      }
    }
    return TYPE_NOT_CONNECTED;
  }

  public static boolean isNetworkConnected(Context context) {
    int networkStatus = getConnectivityStatus(context);
    if (networkStatus == TYPE_WIFI || networkStatus == TYPE_MOBILE) {
      return true;
    } else {
      return false;
    }
  }
}

Use like this:

if(NetworkUtils.isNetworkConnected(this)){

}



回答2:


use the ConnectivityManager class. here is the java class:

 ConnectivityManager manager =(ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){
                //we have WIFI
            }
            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){
                //we have cellular data
            }
        } else{
            //we have no connection :(
        }

do not forget to ask for the permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


来源:https://stackoverflow.com/questions/42350780/how-to-detect-internet-connection-in-android-studio

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