Android开发笔记: Android获取Ip地址

public  String getLocalIpAddress() {
    
    //获取wifi服务
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    //判断wifi是否开启
    if (!wifiManager.isWifiEnabled()) {
        return "NULL";
    }
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return  (ip & 0xFF ) + "." + ((ip >> 8 ) & 0xFF) + "." + ((ip >> 16 ) & 0xFF) + "." + ( ip >> 24 & 0xFF) ;
}

猜你喜欢

转载自blog.csdn.net/taoerit/article/details/79975303