【转】获取手机的ipv4地址
2016-05-09 15:39
459 查看
http://blog.csdn.net/yueqinglkong/article/details/17391051
直接贴代码:
[java] view plain copy
print?
public class GetLocalIpAddress extends Activity implements OnClickListener {
private TextView iplocal;
private Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.acy_showipaddress);
init();
}
public void init() {
iplocal = (TextView) findViewById(R.id.tv_ipaddress);
click = (Button) findViewById(R.id.btn_click);
click.setOnClickListener(this);
}
public String GetIp() {
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();
// ipv4地址
if (!inetAddress.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(inetAddress
.getHostAddress())) {
return inetAddress.getHostAddress();
}
}
}
} catch (Exception ex) {
}
return "";
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == click) {
iplocal.setText(GetIp().toString());
}
}
}
界面是添加的一个button和textview ,就不给xml了。
注意:
1.获取的地址分ipv4和ipv6地址,你需要加个判断获取ipv4的地址。
直接贴代码:
[java] view plain copy
print?
public class GetLocalIpAddress extends Activity implements OnClickListener {
private TextView iplocal;
private Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.acy_showipaddress);
init();
}
public void init() {
iplocal = (TextView) findViewById(R.id.tv_ipaddress);
click = (Button) findViewById(R.id.btn_click);
click.setOnClickListener(this);
}
public String GetIp() {
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();
// ipv4地址
if (!inetAddress.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(inetAddress
.getHostAddress())) {
return inetAddress.getHostAddress();
}
}
}
} catch (Exception ex) {
}
return "";
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == click) {
iplocal.setText(GetIp().toString());
}
}
}
界面是添加的一个button和textview ,就不给xml了。
注意:
1.获取的地址分ipv4和ipv6地址,你需要加个判断获取ipv4的地址。
相关文章推荐
- 指针参数
- php实现的web采集神器
- coreData
- <td></td>标签的border 样式在浏览器中显示不出来的解决方法
- contains duplicate 3
- 函数式编程思想:耦合和组合,第1部分
- hrbust/哈理工oj 1617 回家【BFS+BFS】
- Android 热补丁动态修复框架小结
- C++第五次作业
- C++第5次上机实验—数组分离
- (4.1.11.2)RecyclerView初探
- 主成分分析PCA工作原理和简单介绍
- QT采用MQTT协议远程控制LED灯
- Type 'JNIEnv' could not be resolved
- jquery动态插入append、prepend、before、after区别
- 自己写的一个php基于phpQuery的通用采集类
- 侧栏工具条开发
- 4.27日第15次作业,论文及架构,投资大3000W,周期大2年
- vxlan和IPsec结合使用
- Android Gradle使用详解