需添加如下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
/**
* 获得ip地址【Old版本,在MX351手机上获取的ip地址有错】,修改by GeXianglin
* @param context
* @return
* @throws SocketException
*/
public static String getIPAddress(Context context) throws SocketException {
String ret = null;
if ((ret = getIPAddressPrivate(context)) != null) { // 主要处理wifi
return ret;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { // 主要处理3G网络
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() && (inetAddress instanceof Inet4Address)) {
return inetAddress.getHostAddress().toString();
}
}
}
} else {
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().toString();
}
}
}
}
return null;
}
/*
* 获取Ip地址,原来的ip地址获取,在魅族MX351手机上获取ip地址有错,增加这个在老方法里头。
* @param context
* @return
* @author GeXianglin
*/
private static String getIPAddressPrivate(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifiManager.getDhcpInfo();
if (dhcp != null) {
int ipAddress = dhcp.ipAddress;
return (ipAddress & 0xff) + "." + ((ipAddress >> 8) & 0xff) + "." +
((ipAddress >> 16) & 0xff) + "." + ((ipAddress >> 24) & 0xff);
}
return null;
}
除了上面的DHCP方法,还可以使用wifiManager.getWifiInfo().getIpaddress()方法获取Ip地址。
// 获取手机WIFI的ip地址
public static String getLocalIpAddress() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress(); // 获取32位整型IP地址
//返回整型地址转换成“*.*.*.*”地址
return String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
}
// 获取3G网络的Ip地址
public static String getIpAddress() {
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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 获取本地ip
public String getLocalIpAddress() {
String ipAddress = null;
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface iface : interfaces) {
if (iface.getDisplayName().equals("eth0")) {
List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
for (InetAddress address : addresses) {
if (address instanceof Inet4Address) {
ipAddress = address.getHostAddress();
}
}
} else if (iface.getDisplayName().equals("wlan0")) {
List<InetAddress> addresses = Collections.list(iface.getInetAddresses());
for (InetAddress address : addresses) {
if (address instanceof Inet4Address) {
ipAddress = address.getHostAddress();
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipAddress;
}