在开发过程中,有时候需要考虑到网络状态,然后弹出一个提示框,特别是在弱网的情况下,为了更好的用户体验,一般都是需要一个提示页面的。
这里面关于网络的判断,包含网络是否可用,超时状态,2G网络、3G网络、是否是wifi状态等。
有时候我们请求一个接口,在网络不稳定,弱网的情况下,就很容易出现请求超时的情况,当然这个超时时间是我们设定的,一般都是30秒,算差不多了。
另外对于目前已经出现5G的时代,2G、3G几乎以为时代淘汰了,所以2G、3G其实多可以看作是弱网处理。
至于wifi,我们用的app中,存在这样的逻辑过程,当在使用app的时候,手机突然连接上一个wifi,这时app会弹出一个提示框,显示接入某个网络,确认是否需要继续使用的情况,这种其实是安全保护的一种,毕竟现在通过wifi窃取他人信息还是存在的,会有这个业务逻辑一般是银行类app,为了确保交易等的安全性,会考虑这种做法。
所以关于网络状态的获取还是很多必要的,还是有使用场景的。下面是一个网络状态的工具类,注释很清晰。
public class NetworkUtil {
private static final String TAG = "NetworkUtil";
public static int NET_CNNT_BAIDU_OK = 1; // NetworkAvailable
public static int NET_CNNT_BAIDU_TIMEOUT = 2; // No NetworkAvailable
public static int NET_NOT_PREPARE = 3; // Net No Ready
public static int NET_ERROR = 4; // Net Error
private static int TIMEOUT = 3000; // TIMEOUT
/**
* check NetworkAvailable
*
* @param context 这里一般传入getApplicationContext
* @return
*/
public static boolean isNetworkAvailable(Context context) {
if (context == null) return false;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null == manager) {
return false;
}
NetworkInfo info = manager.getActiveNetworkInfo();
return !(null == info || !info.isAvailable());
}
/**
* get local ip address
*
* @return
*/
public static String getLocalIpAddress() {
String localIp = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface ni = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = ni.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
localIp = inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return localIp;
}
/**
* get net state
*
* @param context 这里一般传入getApplicationContext
* @return
*/
public static int getNetState(Context context) {
try {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo networkinfo = connectivity.getActiveNetworkInfo();
if (networkinfo != null) {
if (networkinfo.isAvailable() && networkinfo.isConnected()) {
if (!connectionNetwork()) {
return NET_CNNT_BAIDU_TIMEOUT;
} else {
return NET_CNNT_BAIDU_OK;
}
} else {
return NET_NOT_PREPARE;
}
}
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
return NET_ERROR;
}
/**
* ping "www.baidu.com"
*
* @return
*/
private static boolean connectionNetwork() {
boolean result = false;
HttpURLConnection httpUrl = null;
try {
httpUrl = (HttpURLConnection) new URL("www.baidu.com").openConnection();
httpUrl.setConnectTimeout(TIMEOUT);
httpUrl.connect();
result = true;
} catch (IOException e) {
} finally {
if (null != httpUrl) {
httpUrl.disconnect();
}
httpUrl = null;
}
return result;
}
/**
* check is 2G
*
* @return boolean
*/
public static boolean is2G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
return activeNetInfo != null && (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS || activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA);
}
/**
* check is3G
*
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
return activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}
/**
* check is Wifi
*
* @return boolean
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
return activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
}