1--安卓网络编程之获取IP地址

关于IP

1.IP 网际协议地址(Internet Protocol Address)
2.是TCP/IP协议族中网络层的协议
3.互联网上的每一个网络和每一台主机分配一个逻辑地址,以此来屏蔽物理地址的差异

IPv4

1.IP协议第四版
2.位数为32位,即32个0,1排列组合形成的不同数值,每个数值可对应Internet的一个逻辑地址,共2的32次方个。
3.每个逻辑地址可供一个设备连接到Internet,通过这个逻辑地址就能寻找到某个设备。


获取手机的IPv4
9414344-e2405febdfb1592f.png
ipv4安卓测试.png
仅开启移动数据时:
{ccmni0=10.66.253.0, 
lo=127.0.0.1}

开启手机热点时:
{ccmni0=10.66.253.0, 
lo=127.0.0.1, 
ap0=192.168.43.1}

连上WLAN(无线时):
{Mac=1c:77:f6:11:1e:4e, 
name="FAST_4BB",
ip=192.168.10.111, 
BSSID=d8:15:0d:4b:b7:a0}

可见手机流量走的是:ccmni0名称的ip
手机热点会产生一个ip: ap0名称的ip
都会有一个本地的ip地址:127.0.0.1


代码实现:这里用Properties盛放信息

判断网络类型需要的对象:NetworkInfo对象info
获取方法:通过Context获取系统服务ConnectivityManagergetActiveNetworkInfo()
通过info.getType()
移动网络2G/3G/4G:ConnectivityManager.TYPE_MOBILE
WIFI:ConnectivityManager.TYPE_WIFI

移动网络IP:获取NetworkInterface对象
获取NetworkInterface迭代枚举nis:NetworkInterface.getNetworkInterfaces()
获取InetAddress迭代枚举ias:ni.getInetAddresses

WIFI:获取WifiInfo对象
获取方法:通过Context获取系统服务WifiManagergetConnectionInfo()

    /**
     * 获取IPv4
     *
     * @param context
     * @return
     */
    public static Properties getIPAddress(Context context) {
        //将ip信息保存在Properties对象中
        Properties prop = new Properties();
        //获取网络信息
        NetworkInfo info = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        //网络信息非空,并且已连接
        if (info != null && info.isConnected()) {
            //当前使用2G/3G/4G网络
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                try {
                    //获取NetworkInterface的迭代枚举
                    Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                    while (nis.hasMoreElements()) {//开始迭代
                        NetworkInterface ni = nis.nextElement();//获取每一个NetworkInterface
                        //获取InetAddress的迭代枚举
                        Enumeration<InetAddress> ias = ni.getInetAddresses();
                        while (ias.hasMoreElements()) {//开始迭代
                            InetAddress ia = ias.nextElement();//获取每一个InetAddress
                            //只取IPv4
                            if (ia instanceof Inet4Address) {
                                //将属性设置到集合
                                prop.setProperty(ni.getDisplayName(), ia.getHostAddress());
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }

            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                //通过WIFI管理者获取Wifi信息
                WifiInfo wifiInfo = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
                        .getConnectionInfo();
                //将属性设置到集合
                prop.setProperty("ip", int2StrIP(wifiInfo.getIpAddress()));//得到IPV4地址
                prop.setProperty("name", wifiInfo.getSSID());
                prop.setProperty("BSSID", wifiInfo.getBSSID());
                prop.setProperty("Mac", wifiInfo.getMacAddress());
            }
        } else {
            //当前无网络连接
            prop.setProperty("ERROR", "请打开网络");
        }
        return prop;
    }
/**
 * 将得到的int类型的IP转换为String类型
 *
 * @param ip
 * @return
 */
public static String int2StrIP(int ip) {
    return (ip & 0xFF) + "." +
            ((ip >> 8) & 0xFF) + "." +
            ((ip >> 16) & 0xFF) + "." +
            (ip >> 24 & 0xFF);
}
使用:
Properties prop = IpUtils.getIPAddress(IPActivity.this);
Enumeration<?> names = prop.propertyNames();
while (names.hasMoreElements()) {
    String s = (String) names.nextElement();
    log.e("TAG",(s + "=" + prop.getProperty(s)) + "\n");
}
9414344-390a49f6d9c12c54.png
debug查看安卓网络信息.png

后记:捷文规范
1.本文成长记录及勘误表
项目源码日期备注
V0.1--无2018-10-141--安卓网络编程之获取IP地址
2.更多关于我
笔名QQ微信爱好
张风捷特烈1981462002zdl1994328语言
我的github我的简书我的优快云个人网站
3.声明

1----本文由张风捷特烈原创,转载请注明
2----欢迎广大编程爱好者共同交流
3----个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
4----看到这里,我在此感谢你的喜欢与支持

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张风捷特烈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值