安卓获取本机IP和网关IP

String getIP() {
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int i = wifiInfo.getIpAddress();
    return (i & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF)+ "." + ((i >> 24 ) & 0xFF );
}

String getGateway() {
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    DhcpInfo dhcp = wifiManager.getDhcpInfo();
    int i = dhcp.gateway;
    return (i & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF)+ "." + ((i >> 24 ) & 0xFF );
}

你可能感兴趣的:(android,java)