在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+的更多相关文章

  1. 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 ...

  2. 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 ...

  3. IP Address 分类: POJ 2015-06-12 19:34 12人阅读 评论(0) 收藏

    IP Address Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19125   Accepted: 11053 Desc ...

  4. "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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【译】Android 6.0 Changes (机翻加轻微人工校对)

    Android 6.0 Changes In this document Runtime Permissions Doze and App Standby Apache HTTP Client Rem ...

  9. Ubuntu setup Static IP Address

    Change Ubuntu Server from DHCP to a Static IP Address If the Ubuntu Server installer has set your se ...

随机推荐

  1. Temporary ASP.NET 拒绝访问

    CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\dd813f66 ...

  2. linux服务器默认连接数配置

    vi /etc/security/limits.d/90-nproc.conf * - nofile 65536* - nproc 65536root soft nproc unlimited vi ...

  3. Flowplayer-Subtitle

    SOURCE URL: https://flowplayer.org/docs/subtitles.html Setting up Subtitles are loaded with a <tr ...

  4. spark配置

    -Dspark.master=local -Xms128m -Xmx512m -XX:MaxPermSize=300m -ea

  5. CentOS安装Nvidia显卡驱动提示Nouveau正在使用的问题

    如题,在安装Nvidia官方提供的驱动时,提示Nouveau正在被使用,需要停用后才能继续安装,在网上搜了不少方法,各种尝试后均无效. 最后通过设置内核启动参数给屏蔽了,方法如下: 切换到root用户 ...

  6. Maven学习(一) -- 安装Maven及Eclipse中配置Maven

    标签(空格分隔): 学习笔记 本文环境:Windows7, JDK1.7.0_76 安装及配置Maven环境变量 需要电脑中已经有Java环境 在控制台中输入:echo %JAVA_HOME%看是否能 ...

  7. centos7 firewall 防火墙 命令

    为了架设ss在vultr上买了一个日本的vps 用的是centos7的系统 防火墙是 firewall 捣鼓了两天 在这里总结一下. 如果小伙伴也准备在vultr上买vps  在注册是 可以使用这个优 ...

  8. 后勤数据源增量队列Delta Queue(RSA7)中的增量更新区Delta Update、增量重复区Delta Repetition

    声明:原创作品,转载时请注明文章来自SAP师太技术博客:( 博/客/园www.cnblogs.com)www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. Analyzer报表结果行

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. CentOS6.5 mysql 5.5安装

    CentOS 6 mysql5.5安装配置 1 安装所需软件 2 安装cmake 3 tar.gz形式安装mysql 4 配置与启动 MySQL自5.5版本以后,就开始使用cmake编译工具了.tar ...