zoukankan      html  css  js  c++  java
  • android 获取手机ip的三种方式

    android 获取手机ip的方式

    第一,通过WifiManager获取

    private String getLocalIPAddress (Context context) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ipAddress = FormatIP(wifiInfo.getIpAddress());
                return ipAddress;
    }
    
    public String FormatIP (int ip) {
                return Formatter.formatIpAddress(ip);
    }

     第二,通用的方式java.net.networkinterface

    private String getLocalIPAddress() {
                try {
                    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() && inetAddress instanceof Inet4Address) {
                                // return inetAddress.getAddress().toString();
                                return inetAddress.getHostAddress().toString();
                            }
                        }
                    }
                } catch (SocketException ex) {
                    Log.e("BaseScanTvDeviceClient", "获取本机IP false =" +  ex.toString());
                } 
    
                return null;
            }
  • 相关阅读:
    查询论文引用次数及格式和相似论文的方法
    JAVA日期加减运算
    luogu2833 等式
    luogu2261 [CQOI2007] 余数之和
    luogu2822 组合数问题
    luogu3942 将军令 贪心
    luogu3941 入阵曲
    luogu3939 数颜色
    二分查找总结
    luogu3938 斐波那契
  • 原文地址:https://www.cnblogs.com/zhangyulogin/p/2687313.html
Copyright © 2011-2022 走看看