NetUtil

public class NetUtil {
    private NetUtil() {
    }

    /**
     * 获取ip前缀
     * 
     * @return
     */
    public static String getLocalIpAddressPrefix(Context context) {
        String ipAddress = getLocalIpAddress(context);
        return ipAddress.substring(0, ipAddress.lastIndexOf(".") + 1);
    }

    /**
     * 获取本机IP
     * 
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getLocalIpAddress(Context context) {
        WifiInfo wifiInfo = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo();
        return Formatter.formatIpAddress(wifiInfo.getIpAddress());
    }
}

 

posted @ 2014-03-26 16:53  bingoogolapple  阅读(274)  评论(0)    收藏  举报