get WiFi LYIP - Android Phone

Android examples for Phone:wifi

Description

get WiFi LYIP

Demo Code


//package com.java2s;
import android.content.Context;

import android.net.wifi.WifiManager;
import java.net.Inet4Address;

public class Main {

    @SuppressWarnings("deprecation")
    public static String getWiFiLYIP(Context context) {
        WifiManager mWifiManager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        String ip = ipIntToString(mWifiManager.getDhcpInfo().gateway);
        return "http://" + ip;
    }//from ww w . j  a  v  a 2s  . c o m

    private static String ipIntToString(int ip) {
        try {
            byte[] bytes = new byte[4];
            bytes[0] = (byte) (0xff & ip);
            bytes[1] = (byte) ((0xff00 & ip) >> 8);
            bytes[2] = (byte) ((0xff0000 & ip) >> 16);
            bytes[3] = (byte) ((0xff000000 & ip) >> 24);
            return Inet4Address.getByAddress(bytes).getHostAddress();
        } catch (Exception e) {
            return "";
        }
    }
}

Related Tutorials