检测网络的一个工具包:
1、网络是否可用;
2、判断是否有网络连接;
3、判断 WIFI 网络是否可用;
4、判断 MOBILE 网络是否可用;
5、获取当前网络连接的类型信息;
6、获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo; /**
* @Title NetWorkUtil
* @Description 是检测网络的一个工具包
* @author 淡定
*/
public class NetWorkUtil { public static enum NetType {
WIFI, CMNET, CMWAP, NoneNet
} /**
* 网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) { ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
} /**
* 判断是否有网络连接
*
* @param context
* @return
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo 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) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
} /**
* 判断 MOBILE 网络是否可用
*
* @param context
* @return
*/
public static boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null) {
return mMobileNetworkInfo.isAvailable();
}
}
return false;
} /**
* 获取当前网络连接的类型信息
*
* @param context
* @return
*/
public static int getConnectedType(Context context) { if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
} /**
* 获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap 网络3:net网络
*
* @param context
* @return
*/
public static NetType getAPNType(Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return NetType.NoneNet;
}
int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) {
if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
return NetType.CMNET;
} else {
return NetType.CMWAP;
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
return NetType.WIFI;
}
return NetType.NoneNet; }
}
NetworkUtil.java
package com.oliver.tcpsocket; import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; public class NetworkUtil { /**
*判断wifi是否连接
* @param context
* @return
*/
public static boolean isWiFiConnected(Context context){ ConnectivityManager connectManager =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo =
connectManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(networkInfo.isConnected()){
return true;
}
else{
return false;
}
} /**
* 得到wifi连接的IP地址
* @param context
* @return
*/
public static String getWifiIP(Context context){ WifiManager wifiManager =
(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddr = wifiInfo.getIpAddress();
String ipStr = int2string(ipAddr);
return ipStr;
} /**
* 输入int 得到String类型的ip地址
* @param i
* @return
*/
private static String int2string(int i){ return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF ); }
}