获取手机IP地址
2015-07-21 18:00
225 查看
获取手机IP地址
第一种:
public int getIpAddress() {
WifiManager wifiManager = (WifiManager) this.context
.getSystemService(Context.WIFI_SERVICE);
// 判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
return ipAddress;
}
private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + (i >> 24 & 0xFF);
}
第二种:
public String getInetIpAddress() {
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();
return inetAddress.getHostAddress();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
转:http://www.cnblogs.com/crearo-ssy/archive/2012/08/16/2640612.html
第一种:
public int getIpAddress() {
WifiManager wifiManager = (WifiManager) this.context
.getSystemService(Context.WIFI_SERVICE);
// 判断wifi是否开启
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
return ipAddress;
}
private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF)
+ "." + (i >> 24 & 0xFF);
}
第二种:
public String getInetIpAddress() {
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();
return inetAddress.getHostAddress();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
转:http://www.cnblogs.com/crearo-ssy/archive/2012/08/16/2640612.html
相关文章推荐
- uva 11235 - Frequent values(RMQ)
- python time module
- A+B for Input-Output Practice (II)
- coj_1022菜鸟与大牛
- 免安装Oracle客户端使用(转)
- jquery 选择器
- iOS 通讯录-获取联系人属性
- 小学期acm训练第一发(简单数学题)
- Apache Ant运行时Unable to locate tools.jar解决方法
- 常用正则表达式
- mybatis返回List<Map<String,Object>>
- AsyncTask 和 Handler
- nginx优化 突破十万并发
- 深入剖析Android音频之AudioTrack
- Qt、C++小票打印----网口
- 使用Cygwin在Windows上体验Linux的快感
- Ansible安装过程中遇到过的问题
- Android Studio 使用技巧(一)
- vs2010编译某个工程提示缺少lib或者头文件 vs2010的配置中的生成事件
- linux下搭建svn版本控制软件