package com.basic.security.utils;
|
|
import android.content.Context;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
|
import com.basic.security.base.BaseApplication;
|
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.util.Collections;
|
import java.util.List;
|
|
public class NetUtil {
|
/**
|
* 判断是否有网络连接
|
*
|
* @return
|
*/
|
public static boolean isNetworkConnected() {
|
ConnectivityManager mConnectivityManager = (ConnectivityManager) BaseApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo mNetworkInfo = null;
|
if (mConnectivityManager != null) {
|
mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
|
}
|
if (mNetworkInfo != null) {
|
return mNetworkInfo.isAvailable();
|
}
|
return false;
|
}
|
|
/**
|
* 判断WIFI网络是否可用
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean isWifiConnected(Context context) {
|
if (context != null) {
|
ConnectivityManager mConnectivityManager = (ConnectivityManager) BaseApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo mWiFiNetworkInfo = null;
|
if (mConnectivityManager != null) {
|
mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
}
|
if (mWiFiNetworkInfo != null) {
|
return mWiFiNetworkInfo.isAvailable();
|
}
|
}
|
return false;
|
}
|
|
public static boolean isWiFiActive() {
|
ConnectivityManager connectivity = (ConnectivityManager) BaseApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
|
if (connectivity != null) {
|
NetworkInfo[] info = connectivity.getAllNetworkInfo();
|
if (info != null) {
|
for (NetworkInfo anInfo : info) {
|
if (anInfo.getTypeName().equals("WIFI") && anInfo.isConnected()) {
|
return true;
|
}
|
}
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 获取当前网络连接的类型信息
|
*
|
* @return
|
*/
|
public static int getConnectedType() {
|
if (BaseApplication.getApplication() != null) {
|
ConnectivityManager mConnectivityManager = (ConnectivityManager) BaseApplication.getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo mNetworkInfo = null;
|
if (mConnectivityManager != null) {
|
mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
|
}
|
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
|
return mNetworkInfo.getType();
|
}
|
}
|
return -1;
|
}
|
|
public static String getIPAddress(boolean useIPv4) {
|
try {
|
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
|
for (NetworkInterface intf : interfaces) {
|
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
|
for (InetAddress addr : addrs) {
|
if (!addr.isLoopbackAddress()) {
|
String sAddr = addr.getHostAddress();
|
//boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
|
boolean isIPv4 = sAddr.indexOf(':') < 0;
|
if (useIPv4) {
|
if (isIPv4)
|
return sAddr;
|
} else {
|
if (!isIPv4) {
|
int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
|
return delim < 0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
|
}
|
}
|
}
|
}
|
}
|
} catch (Exception ignored) {
|
} // for now eat exceptions
|
return "";
|
}
|
}
|