如何在Android 10及以上版本中检测网络类型



我已经在我的项目中实现了网络类型提取,但在Android 10中它不起作用,我试图找到解决方案,但没有成功。这是我的代码,但它总是返回空白(未知类型(

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
printLog("networkType","-"+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: {
showToast(context, "Connection Available is 2G");
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: {
showToast(context, "Connection Available is 3G");
return "3G";
}
case TelephonyManager.NETWORK_TYPE_LTE: {
showToast(context, "Connection Available is 4G");
return "4G";
}
case TelephonyManager.NETWORK_TYPE_NR:
{
showToast(context, "Connection Available is 5G");
return "5G";
}
default: {
showToast(context, "Not detect");
return "";
}
}

要在android 10或更高版本中获取网络类型,请使用mTelephonyManager.getDataNetworkType((;已安装mTelephonyManager.getNetworkType((;

这是工作函数:

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//    ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                          int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
//return TODO;
networkType = mTelephonyManager.getDataNetworkType();
}
} else {
networkType = mTelephonyManager.getNetworkType();
}
printLog("networkType", "-" + 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: {
showToast(context, "Connection Available is 2G");
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: {
showToast(context, "Connection Available is 3G");
return "3G";
}
case TelephonyManager.NETWORK_TYPE_LTE: {
showToast(context, "Connection Available is 4G");
return "4G";
}
case TelephonyManager.NETWORK_TYPE_NR: {
showToast(context, "Connection Available is 5G");
return "5G";
}
default: {
showToast(context, "Not detect");
return "";
}
}
}

最新更新