在 Android 中获取网络连接警告

Get Network connection warning in Android

我正在尝试以编程方式检测,当 Android phone 触发此“警告”时,已建立的互联网连接目前似乎无法访问(在本例中:Edge / 2G) ,像这样:

确实出现这个警告时,无法访问互联网,没有它就可以了。

我目前正在做的是检测正确的网络:

public String getNetworkClass() {

    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        return "Unknown";
    } else {
        int networkType = mTelephonyManager.getNetworkType();
        Log.i("NETWORK", String.valueOf(networkType));
        mobileNetworkType = networkType;

        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "2G";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return "3G";
            case TelephonyManager.NETWORK_TYPE_LTE:
                return "4G";
            case TelephonyManager.NETWORK_TYPE_NR:
                return "5G";
            default:
                return "Unknown";
        }
    }
}

2G的时候,我在测下载/上传速率。我让自己 猜测下载速率低于 150 kbps 可能会导致“警告”:

if(Arrays.asList(new String[]{"2G"}).contains(getNetworkClass())) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
    int downSpeed = nc.getLinkDownstreamBandwidthKbps();
    int upSpeed = nc.getLinkUpstreamBandwidthKbps();
    

    if(downSpeed < 150) {
        // I assume the connection is bad here                            
    } else {
        // All fine!
    }

但是,我的测量值在30到100kbps之间(每五秒一次),有时在这个范围内,有时会出现警告,有时不会。

我怎样才能成功捕捉到这个?有没有 Android 接口来检查这个?如果可能的话,我想避免 ping 检查,如:

public boolean canAccessInternet() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = ipProcess.waitFor();
        System.out.println("Network mExitValue " + exitValue);
        return (exitValue == 0);
    } catch (IOException e) {
        System.out.println(" IO Error ");
        e.printStackTrace();
    } catch (InterruptedException e) {
        System.out.println(" Interrupted Error ");
        e.printStackTrace();
    }
    return false;
}

A​​FAIK,您在连接图标上使用感叹号引用的用户体验是由网络功能驱动的 NET_CAPABILITY_VALIDATED (link)。

Indicates that connectivity on this network was successfully validated. For example, for a network with NET_CAPABILITY_INTERNET, it means that Internet connectivity was successfully detected.

因此在您的代码中,您可以将其更新为:

...
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
    Context.CONNECTIVITY_SERVICE);
NetworkCapabilities nc = cm.getNetworkCapabilities(cm.getActiveNetwork());
if(!nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
    // Show the exclamation point
}