现在的位置: 首页 > 综合 > 正文

android中网络判断

2018年05月10日 ⁄ 综合 ⁄ 共 1040字 ⁄ 字号 评论关闭

判断WIFI是否打开
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mgrTel = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE);

return ((mgrConn.getActiveNetworkInfo() != null && mgrConn .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);

}

判断是否是3G网络
public static boolean is3rd(Context context) {
ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null && networkINfo.getType() == ConnectivityManager.TYPE_MOBILE) {

return true;
}
 return false;
}

判断是否是wifi网络
public static boolean isWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo networkINfo = cm.getActiveNetworkInfo();
if (networkINfo != null && networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {

return true;
} return false;
}

 

抱歉!评论已关闭.