public class NetUtils {
// 網(wǎng)絡(luò)是否可用
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
// 得到當(dāng)前網(wǎng)絡(luò)類型
public static String getNetWorkType(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
int type = networkInfo.getType();
if (type == ConnectivityManager.TYPE_WIFI) {
return "wifi";
} else if (type == ConnectivityManager.TYPE_MOBILE) {
return "mobile";
}
return "unknow";
}
//當(dāng)前網(wǎng)絡(luò)是不是wifi
public static boolean isConnectedByWifi(Context context) {
try {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();
return (ni.getType() == ConnectivityManager.TYPE_WIFI ? true : false);
} catch (Exception e) {
}
return false;
}
// MOBILE網(wǎng)絡(luò)是幾G網(wǎng)2G?3G?4G?
// 返回幾就是幾G網(wǎng)
public static int getNetWorkClass(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch (telephonyManager.getNetworkType()) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return 2;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return 3;
case TelephonyManager.NETWORK_TYPE_LTE:
return 4;
default:
return 0;// unknow
}
}
// wifi網(wǎng)絡(luò)是否可用
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網(wǎng)絡(luò)是否可用
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;
}
// 使用MOBILE網(wǎng)絡(luò)時(shí)用來獲取IP
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
return inetAddress.getHostAddress().toString();
}
}
} catch (SocketException ex) {
}
return "0.0.0.0";
}
// 使用Wifi時(shí)用來獲取IP
public static String getWifiIpAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
return intToIp(ipAddress);
}
// 獲取wifi的ssid(即wifi名稱)
public static String getWifiSsid(Context context) {
String ssidApn = "";
try {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wm.getConnectionInfo();
ssidApn = wi.getSSID();
} catch (Exception e) {
}
return ssidApn;
}
// 手機(jī)GPS是否開啟
public static boolean isGpsEnable(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
// 返回運(yùn)營(yíng)商名字
public static String getNetworkOperatorName(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getNetworkOperatorName();
}
private static String intToIp(int i) {
return String.format("%d.%d.%d.%d", (i & 0xff), (i >> 8 & 0xff), (i >> 16 & 0xff), (i >> 24 & 0xff));
}
}
下面,又來檢測(cè)一下以上的各個(gè)方法:
當(dāng)手機(jī)使用WIFI時(shí):
網(wǎng)絡(luò)是否可用 isNetworkAvailable:true
得到當(dāng)前網(wǎng)絡(luò)類型 getNetWorkType:wifi
當(dāng)前網(wǎng)絡(luò)是不是wifi isConnectedByWifi:true
MOBILE網(wǎng)絡(luò)是幾G網(wǎng) getNetWorkClass:0
wifi網(wǎng)絡(luò)是否可用 isWifiConnected:true
mobile網(wǎng)絡(luò)是否可用 isMobileConnected:true
使用MOBILE網(wǎng)絡(luò)時(shí)用來獲取IP getLocalIpAddress:fe80::fa:70ff:fe07:89d1%dummy0
使用Wifi時(shí)用來獲取IP getWifiIpAddress:192.168.30.6
獲取wifi的ssid(即wifi名稱) getWifiSsid:"***aifu"
手機(jī)GPS是否開啟 isGpsEnable:true
返回運(yùn)營(yíng)商名字 getNetworkOperatorName:中國(guó)聯(lián)通
當(dāng)使用MOBILE網(wǎng)絡(luò)時(shí):
網(wǎng)絡(luò)是否可用 isNetworkAvailable:true
得到當(dāng)前網(wǎng)絡(luò)類型 getNetWorkType:mobile
當(dāng)前網(wǎng)絡(luò)是不是wifi isConnectedByWifi:false
MOBILE網(wǎng)絡(luò)是幾G網(wǎng) getNetWorkClass:3
wifi網(wǎng)絡(luò)是否可用 isWifiConnected:false
mobile網(wǎng)絡(luò)是否可用 isMobileConnected:true
使用MOBILE網(wǎng)絡(luò)時(shí)用來獲取IP getLocalIpAddress:10.34.23.150
使用Wifi時(shí)用來獲取IP getWifiIpAddress:0.0.0.0
獲取wifi的ssid(即wifi名稱) getWifiSsid:<unknown ssid>
手機(jī)GPS是否開啟 isGpsEnable:true
返回運(yùn)營(yíng)商名字 getNetworkOperatorName:中國(guó)聯(lián)通