Android 设备信息(三)

Android 设备信息(三),第1张

Android 设备信息(三)

这篇主要获取网络相关的知识
1: 当前网络连接状态

   public static final int CONNECT_NET_NONE = 0;
    public static final int CONNECT_NET_WIFI = 1;
    public static final int CONNECT_NET_MOBILE = 2;

    
    public static int getConnectNetType(Context context) {
        if (context == null) {
            return CONNECT_NET_NONE;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return CONNECT_NET_NONE;
        }
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (networkInfo == null) {
                return CONNECT_NET_WIFI;
            }
            NetworkInfo.State state = networkInfo.getState();
            if (state == NetworkInfo.State.CONNECTED) {
                return CONNECT_NET_MOBILE;
            } else {
                return CONNECT_NET_WIFI;
            }
        }
        return CONNECT_NET_NONE;
    }

2:判断当前是否联网

  
    public static boolean isNetConnected(Context context) {
        if (context == null) {
            return false;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            return info != null && info.isAvailable();
        }
        return false;
    }

3:获取移动网络下ip地址

  
    public static String getMobileIp() {
        try {
            Enumeration en = NetworkInterface.getNetworkInterfaces();
            if (en == null) {
                return "";
            }
            for (en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface networkInterface = en.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                InetAddress preferredAddress = null;
                for (Enumeration enumeration = networkInterface.getInetAddresses(); enumeration.hasMoreElements(); ) {
                    InetAddress inetAddress = enumeration.nextElement();
                    //skip ipv6 local address
                    if (inetAddress.isLoopbackAddress() || inetAddress.islinkLocalAddress()) {
                        continue;
                    }
                    //prefer ipv4 over ipv6
                    if (inetAddress instanceof Inet4Address) {
                        preferredAddress = inetAddress;
                        break;
                    }
                    //take first ipv6 address
                    if (preferredAddress == null) {
                        preferredAddress = inetAddress;
                    }
                }
                if (preferredAddress != null) {
                    return format(preferredAddress);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String format(InetAddress address) {
        if (address instanceof Inet4Address) {
            return address.getHostAddress();
        } else if (address instanceof Inet6Address) {
            final String ip = address.getHostAddress();
            final int index = ip.indexOf('%');
            if (index >= 0) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        return "";
    }

4:获取wifi下ip

  
    public static String getWifiIp(Context context) {
        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo != null) {
            int ipAddress = wifiInfo.getIpAddress();
            if (ipAddress != 0) {
                return intToIp(ipAddress);
            }
        }
        return "";
    }
    private static String intToIp(int i) {
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

5:获取ip地址

    
    public static String getIp(Context context) {
        try {
            if (getConnectNetType(context) == CONNECT_NET_WIFI) {
                return getWifiIp(context);
            } else {
                return getMobileIp();
            }
        } catch (Throwable e) {
            Log.e("xxxx", "getip :", e);
        }
        return "";
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5583043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存