判断网络
程序员文章站
2022-06-05 20:39:30
...
public static final int NETTYPE_WIFI = 0x01; public static final int NETTYPE_CMWAP = 0x02; public static final int NETTYPE_CMNET = 0x03; public int getNetworkType() { int netType = 0; ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { String extraInfo = networkInfo.getExtraInfo(); if(extraInfo!=null){ if (extraInfo.toLowerCase().equals("cmnet")) { netType = NETTYPE_CMNET; } else { netType = NETTYPE_CMWAP; } } } else if (nType == ConnectivityManager.TYPE_WIFI) { netType = NETTYPE_WIFI; } return netType; }