android ip获取(已经上线使用3年)

版权声明:转载请@我原创地址 https://blog.csdn.net/weixin_39706415/article/details/83691484

    /**
     * 当前ip地址
     *
     * @return String ip
     */
    private static String getIpAddress() {
        try {
            WifiManager wifiManager = (WifiManager) MyApplication.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            assert wifiManager != null;
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            return defaultIp((ipAddress & 0xFF) + "." +
                    ((ipAddress >> 8) & 0xFF) + "." +
                    ((ipAddress >> 16) & 0xFF) + "." +
                    (ipAddress >> 24 & 0xFF));
        } catch (Exception ignored) {

        }
        return defaultIp("");
    }

    private static String defaultIp(String ip) {
        if ("".equals(ip) || ip == null) {
            return "0.0.0.0";
        } else {
            return ip;
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/83691484