Android中通过系统方法判断网络状态以及网络类型
public class NetState {
/**
* Unknown network class
*/
public static final int NETWORK_CLASS_UNKNOWN = 0;
/**
* wifi net work
*/
public static final int NETWORK_WIFI = 1;
/**
* "2G" networks
*/
public static final int NETWORK_CLASS_2_G = 2;
/**
* "3G" networks
*/
public static final int NETWORK_CLASS_3_G = 3;
/**
* "4G" networks
*/
public static final int NETWORK_CLASS_4_G = 4;
/**
* @return 是否有活动的网络连接
*/
public static boolean hasNetWorkConnection(Context context) {
//获取连接活动管理器
final ConnectivityManager connectivityManager = (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//获取链接网络信息
final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isAvailable());
}
public static int getNetWorkClass(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch (telephonyManager.getNetworkType()) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NETWORK_CLASS_2_G;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
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 NETWORK_CLASS_3_G;
case TelephonyManager.NETWORK_TYPE_LTE:
return NETWORK_CLASS_4_G;
default:
return NETWORK_CLASS_UNKNOWN;
}
}
public static int getNetWorkStatus(Context context) {
int netWorkType = NETWORK_CLASS_UNKNOWN;
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
int type = networkInfo.getType();
if (type == ConnectivityManager.TYPE_WIFI) {
netWorkType = NETWORK_WIFI;
} else if (type == ConnectivityManager.TYPE_MOBILE) {
netWorkType = getNetWorkClass(context);
}
}
return netWorkType;
}
}
使用上面的方法可以检测大部分情况下的网络变化,但是在做硬件开发的时候会遇到这种情况,就是Android平板在插上网线之后,网线另一端连通的是路由器。路由器并没有网络,但是路由器到Android平板那一段是连通的,会导致网络判断为有网络。
所以在这种情况下就需要使用特殊的情况去判断网络到底是否连通,比如可以ping服务器来判断网络连接情况。或者检测自己的长连接来做判断。
public static final boolean ping() {
String result = null;
try {
// ping 的地址,可以换成任何一种可靠的外网
String ip = "www.baidu.com";
// ping网址3次
Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);
// 读取ping的内容,可以不加
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer stringBuffer = new StringBuffer();
String content = "";
while ((content = in.readLine()) != null) {
stringBuffer.append(content);
}
Log.d("------ping-----", "result content : " + stringBuffer.toString());
// ping的状态
int status = p.waitFor();
if (status == 0) {
result = "success";
return true;
} else {
result = "failed";
}
} catch (IOException e) {
result = "IOException";
} catch (InterruptedException e) {
result = "InterruptedException";
} finally {
Log.d("----result---", "result = " + result);
}
return false;
}
最后附上一个简易的轮询方法检测网络状态
/**
* 定时轮询柜门状态
*/
public void executeCheckNetwork() {
executorCheckNetwork = Executors.newScheduledThreadPool(1);
executorCheckNetwork.scheduleWithFixedDelay(
new CheckNetworkServer(),
0,
1,
TimeUnit.SECONDS);
}
/**
* 轮询网络状态
*/
class CheckNetworkServer implements Runnable {
@Override
public void run() {
final boolean isConnect = NetState.hasNetWorkConnection(instance);
final int networkStatus = NetState.getNetWorkStatus(instance);
runOnUiThread(new Runnable() {
@Override
public void run() {
//判断网络状态
if (!isConnect) {
ivWlan.setImageDrawable(getResources().getDrawable(R.mipmap.icon_wifi_network_unline));
return;
}
switch (networkStatus) {
case NetState.NETWORK_WIFI:
ivWlan.setImageDrawable(getResources().getDrawable(R.mipmap.icon_network_wifi));
break;
case NetState.NETWORK_CLASS_2_G:
ivWlan.setImageDrawable(getResources().getDrawable(R.mipmap.icon_network_2g));
break;
case NetState.NETWORK_CLASS_3_G:
ivWlan.setImageDrawable(getResources().getDrawable(R.mipmap.icon_network_3g));
break;
case NetState.NETWORK_CLASS_4_G:
ivWlan.setImageDrawable(getResources().getDrawable(R.mipmap.icon_network_4g));
break;
default:
ivWlan.setImageDrawable(getResources().getDrawable(R.mipmap.icon_network_wlan));
break;
}
}
});
}
}