工具类
public class NetworkUtil {
//没有网络连接
public static final int NETWORN_NONE = 0;
//wifi连接
public static final int NETWORN_WIFI = 1;
//手机网络数据连接类型
public static final int NETWORN_2G = 2;
public static final int NETWORN_3G = 3;
public static final int NETWORN_4G = 4;
public static final int NETWORN_MOBILE = 5;
/**
* 获取当前网络连接类型
*
* @param context
* @return
*/
public static int getNetworkType(Context context) {
//获取系统的网络服务
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//如果当前没有网络
if (null == connManager)
return NETWORN_NONE;
//获取当前网络类型,如果为空,返回无网络
NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
return NETWORN_NONE;
}
// 判断是不是连接的是不是wifi
NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (null != wifiInfo) {
NetworkInfo.State state = wifiInfo.getState();
if (null != state)
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
return NETWORN_WIFI;
}
}
// 如果不是wifi,则判断当前连接的是运营商的哪种网络2g、3g、4g等
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (null != networkInfo) {
NetworkInfo.State state = networkInfo.getState();
String strSubTypeName = networkInfo.getSubtypeName();
if (null != state)
if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
switch (activeNetInfo.getSubtype()) {
//如果是2g类型
case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NETWORN_2G;
//如果是3g类型
case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return NETWORN_3G;
//如果是4g类型
case TelephonyManager.NETWORK_TYPE_LTE:
return NETWORN_4G;
default:
//中国移动 联通 电信 三种3G制式
if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {
return NETWORN_3G;
} else {
return NETWORN_MOBILE;
}
}
}
}
return NETWORN_NONE;
}
//判断网络是否连接(是否打开移动网络或者是连接wifi)
public static boolean isNetworkAvailable(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
//判断外网是否可用(常用于wifi已经连接但是无法访问外网的情况),耗时操作,不应该放在主线程
public static boolean isNetworkOnline() {
Runtime runtime = Runtime.getRuntime();
try {
String ip = "www.baidu.com";// 除非百度挂了,否则用这个应该没问题~
Process ipProcess = runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);//ping3次
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return false;
}
}
使用
int networkType = NetworkUtil.getNetworkType(getApplication());
if (networkType == 0){
Toast.makeText(MainActivity.this, "无网络连接", Toast.LENGTH_SHORT).show();
}
if (networkType == 1){
Toast.makeText(MainActivity.this, "当前为wifi连接", Toast.LENGTH_SHORT).show();
}
if (networkType == 2){
Toast.makeText(MainActivity.this, "当前为手机网络连接(2G)", Toast.LENGTH_SHORT).show();
}
if (networkType == 3){
Toast.makeText(MainActivity.this, "当前为手机网络连接(3G)", Toast.LENGTH_SHORT).show();
}
if (networkType == 4){
Toast.makeText(MainActivity.this, "当前为手机网络连接(4G)", Toast.LENGTH_SHORT).show();
}