package com.example.feng.filesync.utility; import android.content.Context; import android.net.ConnectivityManager; import android.net.DhcpInfo; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; /** * Created by feng on 2016/4/7. */ public class NetWorkUtil { /** * 将ip的整数形式转换成ip形式 * * @param ipInt * @return */ public static String int2ip(int ipInt) { StringBuilder sb = new StringBuilder(); sb.append(ipInt & 0xFF).append("."); sb.append((ipInt >> 8) & 0xFF).append("."); sb.append((ipInt >> 16) & 0xFF).append("."); sb.append((ipInt >> 24) & 0xFF); return sb.toString(); } /** * 获取当前ip地址 * * @param context * @return */ public static String getLocalIpAddress(Context context) { try { //获取手机本机ip地址 WifiManager wifiManager = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int i = wifiInfo.getIpAddress(); //获取所连接热点的ip地址 DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); String serverAddress = int2ip(dhcpInfo.serverAddress);
return serverAddress; } catch (Exception ex) { return " 获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n" + ex.getMessage(); } } }