Skip to content

Instantly share code, notes, and snippets.

@ityancs
Created June 3, 2016 06:53
Show Gist options
  • Save ityancs/92ed480902423c3b1b731974aef72856 to your computer and use it in GitHub Desktop.
Save ityancs/92ed480902423c3b1b731974aef72856 to your computer and use it in GitHub Desktop.
NetWork utils for Android to get Android Network Info
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class NetUtil {
//没有网络
public static final int NETWORK_TYPE_INVALID = 0;
//wifi
public static final int NETWORK_TYPE_WIFI = 1;
//移动网路
public static final int NETWORK_TYPE_MOBILE = 2;
public static int getNetWorkType(Context context) {
int mNetworkType = 0;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
String type = networkInfo.getTypeName();
if (type.equalsIgnoreCase("WIFI")) {
mNetworkType = NETWORK_TYPE_WIFI;
} else if (type.equalsIgnoreCase("MOBILE")) {
mNetworkType = NETWORK_TYPE_MOBILE;
}
} else {
mNetworkType = NETWORK_TYPE_INVALID;
}
return mNetworkType;
}
public static boolean isNetWorkInvalid(Context context) {
return getNetWorkType(context) == NETWORK_TYPE_INVALID;
}
public static boolean isWiFi(Context context) {
return getNetWorkType(context) == NETWORK_TYPE_WIFI;
}
public static boolean isMobile(Context context) {
return getNetWorkType(context) == NETWORK_TYPE_MOBILE;
}
public static String getIPAddress(Context context) {
String ipAddress = null;
if (!isNetWorkInvalid(context)) {
if (isWiFi(context)) {
ipAddress = getWIFILocalIpAdress(context);
} else if (isMobile(context)) {
ipAddress = getGPRSLocalIpAddress();
}
}
if (TextUtils.isEmpty(ipAddress)) {
ipAddress = getHostIp();
}
if (!TextUtils.isEmpty(ipAddress) &&
!Patterns.IP_ADDRESS.matcher(ipAddress).matches()) {
return null;
}
return ipAddress;
}
public static String getMacAddress(Context mContext) {
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
return wifiInfo.getMacAddress();
}
public static String getGPRSLocalIpAddress() {
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())
return inetAddress.getHostAddress();
}
}
} catch (SocketException ex) {
Log.e("GPRS IpAddress", ex.toString());
}
return null;
}
public static String getWIFILocalIpAdress(Context mContext) {
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
return formatIpAddress(ipAddress);
}
public static String getHostIp() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr.hasMoreElements(); ) {
InetAddress inetAddress = ipAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (Exception ignored) {
}
return null;
}
private static String formatIpAddress(int ipAdress) {
if (ipAdress == 0) {
return null;
}
return (ipAdress & 0xFF) + "." +
((ipAdress >> 8) & 0xFF) + "." +
((ipAdress >> 16) & 0xFF) + "." +
(ipAdress >> 24 & 0xFF);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment