public class NetworkUtil {
// 判断网络是否可用
public static boolean isNetworkAvailable(Context context) {
// ConnectivityManager:网络连接管理器.
// ①.Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
// 监视网络的连接状态;
// ②.Send broadcast intents when network connectivity changes.
// 当网络连接状态发生改变后发送一个广播意图;
// ③.Attempt to "fail over" to another network when connectivity to a
// network is lost.
// 当一个网络的连接丢失后,尝试着"故障转移"到另一个网络.
// ④.Provide an API that allows applications to query the coarse-grained
// or fine-grained state of the available networks.
// 提供了一套API,供开发者用来查询网络的好与坏的状态;
// ⑤.Provide an API that allows applications to request and select
// networks for their data traffic.
// 提供了一套API,供开发者在网络中进行数据的传递.
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取当前激活的网络信息
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
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;
}
}