//访问网络状态权限
// 判断网络是否可用
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取当前激活的网络信息
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
return info.isAvailable();
}
return false;
}
// 判断网络是否已连接
public static boolean isNetworkConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
return info.isConnected();
}
return false;
}
// 判断有没有连接WiFi网络
public static boolean isWifiConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}
// 判断有没有连接Mobile网络(流量)
public static boolean isMobileConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}
// 判断当前连接的网络类型
public static String getNetworkType(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
// 返回各种网络类型
// int type = info.getType();
// 返回一个人类可读的网络类型名称
return info.getTypeName();
}
return null;
}