IP工具类

Author Avatar
发达 9月 12, 2018
  • 在其它设备中阅读本文章
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @author : www10
 * @Date : 2018/9/11
 * @Description : 网络工具类
 */
public class InternetProtocolUtils {
    /**
     * 获取wifi和网线的ip
     * @param context
     * @return
     */
    public static String getInternetProtocol(Context context) {
        NetworkInfo networkInfo = getNetworkInfo(context);
        if (networkInfo != null && networkInfo.isAvailable()) {
            int type = networkInfo.getType();
            switch (type) {
                case ConnectivityManager.TYPE_WIFI:
                    //wifi
                    return getWifiInternetProtocol(context);
                case ConnectivityManager.TYPE_ETHERNET:
                    //网线
                    return getEthernetInternetProtocol(context);
                default:
                    return "请连接wifi或者插上网线";
            }
        } else {
            return "网络不可用";
        }
    }

    /**
     * 插网线
     *
     * @param context
     * @return
     */
    private static String getEthernetInternetProtocol(Context context) {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
            ex.printStackTrace();
        }
        return "0.0.0.0";
    }

    /**
     * 获取wifi地址
     *
     * @param context
     */
    private static String getWifiInternetProtocol(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService
                (Context.WIFI_SERVICE);
        WifiInfo connectionInfo = null;
        if (wifiManager != null) {
            connectionInfo = wifiManager.getConnectionInfo();
        }
        int ipAddress = 0;
        if (connectionInfo != null) {
            ipAddress = connectionInfo.getIpAddress();
        }
        return intToIp(ipAddress);
    }

    /**
     * 判断网络是否可用
     *
     * @param context
     * @return
     */
    private static boolean networkAvailable(Context context) {
        NetworkInfo networkInfo = getNetworkInfo(context);
        return networkInfo != null && networkInfo.isAvailable();
    }

    /**
     * 获取网络管理类
     *
     * @param context
     * @return
     */
    private static NetworkInfo getNetworkInfo(Context context) {
        ConnectivityManager systemService = (ConnectivityManager) context
                .getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (systemService != null) {
            return systemService.getActiveNetworkInfo();
        } else {
            return null;
        }
    }

    /**
     * int型ip转String
     *
     * @param i
     * @return
     */
    private static String intToIp(int i) {
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }
}