简体   繁体   English

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

[英]How can I check internet connection in Android Q?

Before Android Q all you had to do is use the NetworkUtils class.在 Android Q 之前,您所要做的就是使用 NetworkUtils 类。 But this class was deprecated in API 29. I've been searching for an alternative, and I couldn't seem to find one.但是这个类在 API 29 中已被弃用。我一直在寻找替代方案,但似乎找不到。

So, how can I check the internet connection in Android Q?那么,如何在 Android Q 中检查互联网连接?

Use this code snippet.使用此代码段。

     @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;
}

Happy Coding :)快乐编码:)

If you are looking for a simple code, you can use this:如果你正在寻找一个简单的代码,你可以使用这个:

public static boolean isInternetConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return cm.getActiveNetwork() != null && cm.getNetworkCapabilities(cm.getActiveNetwork()) != null;
    } else {
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }
}

The ConnectivityManager only checks whether the smartphone could theoretically establish an Internet connection. ConnectivityManager 仅检查智能手机理论上是否可以建立 Internet 连接。 Whether an Internet connection actually exists, eg if the network quality is very poor, can only be determined with a ping or a web address. Internet 连接是否真的存在,例如网络质量很差,只能通过 ping 或网址来确定。

Just use this code and call isInternatAvailable(context).只需使用此代码并调用 isInternatAvailable(context)。

    private static final String CMD_PING_GOOGLE = "ping -w -c 1 google.com";

    public static boolean isInternetAvailable(@NonNull Context context) {
        return isConnected(context) && checkInternetPingGoogle();
    }

    public static boolean isConnected(@NonNull Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm != null) {
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        } else {
            return false;
        }
    }

    public static boolean checkInternetPingGoogle(){
        try {
            int a = Runtime.getRuntime().exec(CMD_PING_GOOGLE).waitFor();
            return a == 0x0;
        } catch (IOException ioE){
            EMaxLogger.onException(TAG, ioE);
        } catch (InterruptedException iE){
            EMaxLogger.onException(TAG, iE);
        }
        return false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM