Get IP Address in Android 4.0+
-
在android2.3以下的系统中,可以使用如下的代码来获取Android系统的本地IP地址:
[java]
private String getLocalIPAddress() throws SocketException{
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf = en.nextElement(); www.2cto.com
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
InetAddress inetAddress = enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress())){
return inetAddress.getHostAddress().toString();
}
}
}
return "null";
}但是,在android4.0以上系统中,上面的代码仅能够返回一个ipv6的地址,如果需要获取ip v4的地址,可以这么更改:
[java]
private String getLocalIPAddress() throws SocketException{
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() && <strong>(inetAddress instanceof Inet4Address)</strong>){
return inetAddress.getHostAddress().toString();
}
}
}
return "null";
}需要import的包有:
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
Get IP Address in Android 4.0+的更多相关文章
- dubbo could not get local host ip address will use 127.0.0.1 instead 异常处理
dubbo could not get local host ip address will use 127.0.0.1 instead 查看hostname localhost:spring wls ...
- PRCT-1302 the OCR has an invalid ip address
PRCT-1302 the OCR has an invalid ip address 1. 报错信息 an internal error occurred within cluster verifi ...
- IP Address 分类: POJ 2015-06-12 19:34 12人阅读 评论(0) 收藏
IP Address Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19125 Accepted: 11053 Desc ...
- "cni0" already has an IP address different from 10.244.2.1/24。 Error while adding to cni network: failed to allocate for range 0: no IP addresses available in range set: 10.244.2.1-10.244.2.254
"cni0" already has an IP address different from 10.244.2.1/24. Error while adding to cni n ...
- ZOJ2482 IP Address 2017-04-18 23:11 44人阅读 评论(0) 收藏
IP Address Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose you are reading byte streams fr ...
- ERROR 2003 (HY000): Can't connect to MySQL server on 'ip address' (111)的处理办法
远程连接mysql数据库时可以使用以下指令 mysql -h 192.168.1.104 -u root -p 如果是初次安装mysql,需要将所有/etc/mysql/内的所有配置文件的bind-a ...
- oracle 11g RAC安装节点二执行结果错误CRS-5005: IP Address: 192.168.1.24 is already in use in the network
[root@testdb11b ~]# /u01/app/oraInventory/orainstRoot.sh Changing permissions of /u01/app/oraInvento ...
- 【译】Android 6.0 Changes (机翻加轻微人工校对)
Android 6.0 Changes In this document Runtime Permissions Doze and App Standby Apache HTTP Client Rem ...
- Ubuntu setup Static IP Address
Change Ubuntu Server from DHCP to a Static IP Address If the Ubuntu Server installer has set your se ...
随机推荐
- HTML5 UI框架Kendo UI Web自定义组件(一)
Kendo UI Web包含数百个创建HTML5 web app的必备元素,包括UI组件.数据源.验证.一个MVVM框架.主题.模板等.在Kendo UI Web中如何创建自定义组件呢,在下面的文章中 ...
- TextBox
一.聚焦: private void FrmOnlineChargeMoney_Paint(object sender, PaintEventArgs e) { edtAuthCode.SelectA ...
- centos7忘记root密码修改方式
1.在进入系统选择时按下e键
- MySQL安装(转)
本文介绍MySQL的安装 可以单独阅读,也可以作为PHP环境搭建的一部分 PHP完整配置信息请参考 http://www.cnblogs.com/azhe-style/articles/php_env ...
- php : mysql数据库操作类演示
设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...
- (转载)5分钟安装Linux系统到U盘
一.工具 使用 LinuxLive USB Creator 下载地址:http://xz2.cr173.com//soft/LinuxLiveusb.zip 二.操作步骤 1.下载linux系统镜像, ...
- sqlalchemy 优化count()……
一.sqlalchemy 中的count() count()统计数据特别慢: session.query(cls).count() 8W 数据花费了近50s 但是在数据库中直接查询: select ...
- MFC编程入门之十六(对话框:消息对话框)
前面几节讲了属性页对话框,我们可以根据所讲内容方便的建立自己的属性页对话框.本节讲解Windows系统中最常用最简单的一类对话框--消息对话框. 我们在使用Windows系统的过程中经常会见到消息对话 ...
- [转载]Eclipse提示No java virtual machine
第一次运行Eclipse,经常会提示下面的问题:... No java virtual machine was found after searching the follwing location ...
- contiki-main.c 文件的进程分析
基本进程的创建实例: 共三部分:创建进程.进程自启动和进程的主体部分 /* 声明一个名为hello_world_process和led_process进程 PROCESS 宏实际上声明一个函数并定义一 ...