public class 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
/**
* 判断当前网络是否可用
*/
public static boolean isNetWorkAviliable() {
ConnectivityManager connectivity = (ConnectivityManager) BaseApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null) {
return info.isAvailable();
}
}
return false;
}
/**
* 得到ip地址
*
* @return
*/
public static String getLocalIpAddress() {
String ret = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ret = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return ret;
}
/**
* 返回当前网络状态
*
* @param context
* @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) {
e.printStackTrace();
}
return NET_ERROR;
}
/**
* ping "http://www.baidu.com"
*
* @return
*/
static private boolean connectionNetwork() {
boolean result = false;
HttpURLConnection httpUrl = null;
try {
httpUrl = (HttpURLConnection) new URL("http://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 is3G
*
* @param context
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
/**
* isWifi
*
* @param context
* @return boolean
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* is2G
*
* @param context
* @return boolean
*/
public static boolean is2G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
|| activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS || activeNetInfo
.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
return true;
}
return false;
}
/**
* is wifi on
*/
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}
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
/**
* 判断当前网络是否可用
*/
public static boolean isNetWorkAviliable() {
ConnectivityManager connectivity = (ConnectivityManager) BaseApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null) {
return info.isAvailable();
}
}
return false;
}
/**
* 得到ip地址
*
* @return
*/
public static String getLocalIpAddress() {
String ret = "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ret = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return ret;
}
/**
* 返回当前网络状态
*
* @param context
* @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) {
e.printStackTrace();
}
return NET_ERROR;
}
/**
* ping "http://www.baidu.com"
*
* @return
*/
static private boolean connectionNetwork() {
boolean result = false;
HttpURLConnection httpUrl = null;
try {
httpUrl = (HttpURLConnection) new URL("http://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 is3G
*
* @param context
* @return boolean
*/
public static boolean is3G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}
/**
* isWifi
*
* @param context
* @return boolean
*/
public static boolean isWifi(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* is2G
*
* @param context
* @return boolean
*/
public static boolean is2G(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null
&& (activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE
|| activeNetInfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS || activeNetInfo
.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA)) {
return true;
}
return false;
}
/**
* is wifi on
*/
public static boolean isWifiEnabled(Context context) {
ConnectivityManager mgrConn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager mgrTel = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
}