gpt4 book ai didi

java - 在 Android 中获取网络连接警告

转载 作者:行者123 更新时间:2023-12-05 00:00:25 25 4
gpt4 key购买 nike

我正在尝试以编程方式检测,当 Android 手机触发此“警告”时,已建立的互联网连接当前似乎无法访问(在这种情况下:Edge/2G),如下所示:
Connection warning
事实上,当这个警告出现时,互联网就无法访问,没有它,它就可以工作。
我目前正在做的是检测正确的网络:

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 时,我正在测量下载/上传速率。我为自己定位 here猜测低于 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 到 100 kbps(每五秒一次)之间,有时在此范围内会出现警告,有时不会出现。
我怎样才能成功捕捉到这个?是否有任何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;
}

最佳答案

AFAIK,您在连接图标上显示的感叹号引用的 UX 由网络功能驱动 NET_CAPABILITY_VALIDATED (link)。

Indicates that connectivity on this network was successfullyvalidated. For example, for a network with NET_CAPABILITY_INTERNET, itmeans 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
}

关于java - 在 Android 中获取网络连接警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72359340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com