最近在网上搜索相关知识,发现很乱,有些也不规范。所以整理一下。
仅仅判断是否有网络连接
public static boolean isNetWorkConnected(Context context){
if (null != context){
ConnectivityManager connManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
return info.isAvailable();
}
return false;
}
是否是WiFi连接
public static boolean isWiFiConnected(Context context){
if (null != context){
ConnectivityManager connManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
return info.getState() == NetworkInfo.State.CONNECTED && info.getType() == ConnectivityManager.TYPE_WIFI;
}
return false;
}
是否是手机流量连接
public static boolean isMobileNetWorkConnected(Context context){
if ( null != context){
ConnectivityManager connManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
return info.getState() == NetworkInfo.State.CONNECTED && info.getType() == ConnectivityManager.TYPE_MOBILE;
}
return false;
}