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

2nc8po8w  于 2023-01-07  发布在  Android
关注(0)|答案(4)|浏览(143)

在Android Q之前,您只需使用NetworkUtils类。但API 29中已弃用此类。我一直在寻找替代方法,但似乎找不到。
那么,如何在Android Q中检查互联网连接?

du7egjpx

du7egjpx1#

使用此代码片段。

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

在AndroidManifest.xml中添加以下权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
sr4lhrrt

sr4lhrrt2#

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

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();
    }
}
lc8prwob

lc8prwob3#

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

e0uiprwp

e0uiprwp4#

只需使用这段代码并调用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;
    }

相关问题