##帮助类PhoneNetStateUtil

    package com.funs.PhoneIPAddress.utils;


    /**
     * 手机联网状态工具类 需要的权限 WIFI时:</br>
     * android.permission.ACCESS_WIFI_STATE</br>
     * android.permission.CHANGE_WIFI_STATE </br>
     * android.permission.WAKE_LOCK </br>
     * 手机联网时:</br>
     * android.permission.INTERNET
     */
    public class PhoneNetStateUtil {
        /** 手机网络连接状态管理者 */
        private static ConnectivityManager mConnectivityManager;
        /** 手机网络信息 */
        private static NetworkInfo mNetworkInfo;
        private static Context mContext;
    
        private static void init(Context context) {
            mContext = context;
            mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        }
        /**
         * 判断网络是否可用</br>
         * 必须首先调用该方法,判断网络是否可用。可用再判断是否为WIFI
         */
        public static boolean isNetOk(Context context){
            mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (mConnectivityManager == null) {//判断网络连通管理器是否存在
                return false;
            }
            mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo == null) {//判断网络连接信息是否存在
                return false;
            }
            if (mNetworkInfo.isConnected()) {//判断网络是否连接成功
                return true;
            }
            return false;
        }
    
        /**
         * 判断网络状态是否为WIFI联网
         */
        public static boolean isWIFI(Context context) {
            init(context);
            return (ConnectivityManager.TYPE_WIFI == mNetworkInfo.getType()) ? true
                    : false;
        }
    
        /**
         * 手机连接移动网络时,获取IP地址
         */
        public static String getMobileIpAddress(Context context){
    //        String mobileIp = "";
            StringBuilder mobileIp = new StringBuilder("");
            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()) {
    //                        mobileIp = inetAddress.getHostAddress().toString();
    //                        return mobileIp;
                            mobileIp.append(inetAddress.getHostAddress().toString());
                        }
                    }
    
                }
            } catch (Exception e) {
                Log.e("WifiPreference IpAddress", e.toString());
            }
            if ("".equals(mobileIp)) {
                Toast.makeText(context, "网络连接异常哦亲!", Toast.LENGTH_SHORT).show();
            }
            return mobileIp.toString();
        }
    
        /**
         * 获取本地IP地址,包含了各路由的地址,
         */
        public static String getLocalIpAddress() {
            StringBuilder sbIpAddress = new StringBuilder("");
            try {
                Enumeration<NetworkInterface> en = NetworkInterface
                        .getNetworkInterfaces();
                int one = 0;
                int two = 0;
                while (en.hasMoreElements()) {
                    NetworkInterface ni = en.nextElement();
                    one++;
                    Enumeration<InetAddress> enIp = ni.getInetAddresses();
                    while (enIp.hasMoreElements()) {
                        InetAddress inet = enIp.nextElement();
                        two++;
                        if (!inet.isLoopbackAddress()
                                && (inet instanceof Inet4Address)) {
    //                        return inet.getHostAddress().toString();
                            sbIpAddress.append("/" + inet.getHostAddress().toString());
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return sbIpAddress.toString();
        }
    
        /**
         * 手机wifi联网时,获取内网IP地址
         */
        public static String getWifiInsideIpAddress(Context context){
            init(context);
            WifiManager wifiManager = (WifiManager) mContext
                    .getSystemService(Context.WIFI_SERVICE);
            if (!wifiManager.isWifiEnabled()) {
                wifiManager.setWifiEnabled(true);
            }
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddress = wifiInfo.getIpAddress();
            String insideIp = int2ip(ipAddress);
            return insideIp;
        }
        /**
         * 手机wifi联网时,获取外网IP地址</br>
         * <b>因为该方法需要访问网络数据,属于耗时操作,所以应放到非UI线程中执行</b>
         */
        public static String getWifiOutsideIpAddress() {
            String outsideIp = "";
            try {
                String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";
                URL url = new URL(address);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setUseCaches(false);
    
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream in = connection.getInputStream();
    
                    // 将流转化为字符串
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in));
    
                    String tmpString = "";
                    StringBuilder retJSON = new StringBuilder();
                    while ((tmpString = reader.readLine()) != null) {
                        retJSON.append(tmpString + "\n");
                    }
    
                    JSONObject jsonObject = new JSONObject(retJSON.toString());
                    String code = jsonObject.getString("code");
                    if (code.equals("0")) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        outsideIp = data.getString("ip");
    
                    } else {
                        outsideIp = "";
                        Log.e("提示", "IP接口异常,无法获取IP地址!");
                    }
                } else {
                    outsideIp = "";
                    Log.e("提示", "网络连接异常,无法获取IP地址!");
                }
            } catch (Exception e) {
                outsideIp = "";
                Log.e("提示", "获取IP地址时出现异常,异常信息是:" + e.toString());
            }
            return outsideIp;
        }
        /**
         * 将int型数据转化为ip地址
         * @param value
         */
        private static String int2ip(int value) {
            return (value & 0xFF) + "." + ((value >> 8) & 0xFF) + "."
                    + ((value >> 16) & 0xFF) + "." + ((value >> 24) & 0xFF);
        }
    }