【问题标题】:How can I check internet connection in Android Q?如何在 Android Q 中检查互联网连接?
【发布时间】:2020-03-24 09:06:25
【问题描述】:

在 Android Q 之前,您所要做的就是使用 NetworkUtils 类。但是这个类在 API 29 中被弃用了。 我一直在寻找替代方案,但似乎找不到。

那么,如何在 Android Q 中检查互联网连接?

【问题讨论】:

    标签: java android connection


    【解决方案1】:

    使用此代码 sn-p。

         @IntRange(from = 0, to = 3)
    public static int getConnectionType(Context context) {
        int result = 0; // Returns connection type. 0: none; 1: mobile data; 2: wifi
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (cm != null) {
                NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
                if (capabilities != null) {
                    if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        result = 2;
                    } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        result = 1;
                    } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
                        result = 3;
                    }
                }
            }
        } else {
            if (cm != null) {
                NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                if (activeNetwork != null) {
                    // connected to the internet
                    if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                        result = 2;
                    } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                        result = 1;
                    } else if (activeNetwork.getType() == ConnectivityManager.TYPE_VPN) {
                        result = 3;
                    }
                }
            }
        }
        return result;
    }
    

    快乐编码:)

    【讨论】:

    • 这就是为什么我仔细检查 API 级别查看代码@Remy
    【解决方案2】:

    如果你正在寻找一个简单的代码,你可以使用这个:

    public static boolean isInternetConnected(Context getApplicationContext) {
        boolean status = false;
    
        ConnectivityManager cm = (ConnectivityManager) getApplicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (cm.getActiveNetwork() != null && cm.getNetworkCapabilities(cm.getActiveNetwork()) != null) {
                    // connected to the internet
                    status = true;
                }
    
            } else {
                if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
                    // connected to the internet
                    status = true;
                }
            }
        }
    
        return status;
    }
    

    【讨论】:

      【解决方案3】:

      ConnectivityManager 仅检查智能手机理论上是否可以建立 Internet 连接。 Internet 连接是否实际存在,例如如果网络质量很差,只能通过ping或者网址来判断。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-02
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多