如何检查Android网络连接状态

2024年12月04日 00:43
有1个网友回答
网友(1):

public class NetworkUtil {
/** 网络不可用 */
public static final int NONETWORK = 0;
/** 是wifi连接 */
public static final int WIFI = 1;
/** 不是wifi连接 */
public static final int NOWIFI = 2;

public static int getNetWorkType(Context context) {
if (!isNetWorkAvalible(context)) {
return NetworkUtil.NONETWORK;
}
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting())
return NetworkUtil.WIFI;
else
return NetworkUtil.NOWIFI;
}

public static boolean isNetWorkAvalible(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return false;
}
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null || !ni.isAvailable()) {
return false;
}
return true;