标签:util amp uil java lan sys false content imp
package net.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetCheck {
public static boolean isNetOk(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return isLegacyNetCheck(context);
} else {
return isNewNetCheck(context);
}
}
/**
* SDK API 小于23使用
*
* @return
*/
private static boolean isLegacyNetCheck(Context context) {
return isMobileConnection(context) || isWIFIConnection(context);
}
/**
* 判断手机接入点(APN)是否处于可以使用的状态
* sdk <23
*
* @param context
* @return
*/
private static boolean isMobileConnection(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
、/* 判断当前wifi是否是处于可以使用状态
* sdk <23
*
* @param context
* @return
*/
private static boolean isWIFIConnection(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
/**
* sdk 23以上使用的方法
*
* @param context
* @return
*/
private static boolean isNewNetCheck(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
}
标签:util amp uil java lan sys false content imp
原文地址:https://www.cnblogs.com/flowliver/p/14394009.html