1.
public static boolean checkNet(Context context) {// 获取手机所有连接管理对象(包括对wi-fi,
// net等连接的管理)
try {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
// 获取网络连接管理的对象
NetworkInfo info = connectivity.getActiveNetworkInfo();
//System.out.println(">>>>>>>>>>>>Net:"+info);
if (info == null || !info.isAvailable()){
return false;
}else{
return true;
}
//if (info != null && info.isConnected()) {
判断当前网络是否已经连接
//if (info.getState() == NetworkInfo.State.CONNECTED) {
//return true;
//}
//}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
2.配置AndroidManifest.xml:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public static boolean checkNet(Context context) {// 获取手机所有连接管理对象(包括对wi-fi,
// net等连接的管理)
try {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
// 获取网络连接管理的对象
NetworkInfo info = connectivity.getActiveNetworkInfo();
//System.out.println(">>>>>>>>>>>>Net:"+info);
if (info == null || !info.isAvailable()){
return false;
}else{
return true;
}
//if (info != null && info.isConnected()) {
判断当前网络是否已经连接
//if (info.getState() == NetworkInfo.State.CONNECTED) {
//return true;
//}
//}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
2.配置AndroidManifest.xml:<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />