如何检测三星S10 5G是否正在运行5G网络?

4
Android Q添加了一种新的网络类型,即5G的NETWORK_TYPE_NR,而这种类型在Android Pie上不可用。最近发布的三星S10完全支持5G,当连接到5G网络时,它可以在状态栏上显示5G图标。
第三方应用程序是否有可能知道Android Pie设备是否连接到5G网络?
任何帮助将不胜感激。
以下链接是新网络类型的定义,它在Android Pie分支上不可用。
5个回答

4
我认为他们将代码从Q回溯到Pie,因为5G的逻辑是在去年Q(alpha版)中实现的。 因此,当使用TelephonyManager.getNetworkType()时, 你可能会得到20 (5G)的结果。 编辑 根据下面的评论:网络类型将是13,所以这并没有解决问题。 编辑 尝试使用反射。
static boolean isNRConnected(TelephonyManager telephonyManager) {
    try {
        Object obj = Class.forName(telephonyManager.getClass().getName())
                .getDeclaredMethod("getServiceState", new Class[0]).invoke(telephonyManager, new Object[0]);

        Method[] methods = Class.forName(obj.getClass().getName()).getDeclaredMethods();

        for (Method method : methods) {
            if (method.getName().equals("getNrStatus") || method.getName().equals("getNrState")) {
                method.setAccessible(true);
                return ((Integer) method.invoke(obj, new Object[0])).intValue() == 3;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

你有这款手机和5G网络覆盖吗?你可以尝试调用ServiceState类中的getNrStatus方法。如果返回值为3,则表示已连接到5G网络。 - Pavel Machala
ServiceState类中没有getNrStatus方法,即使在Android Q中也没有。代码将无法编译。 - TZW
你需要使用反射。 - Pavel Machala
不要使用反射,Android 不建议这样做,他们也通过本地 SDK 接口强制执行此规定。 - Piyush Kukadiya
你是对的,使用PhoneStateListener和onDisplayInfo回调是在Android 11中首选的方法。然而,在Android 9和10中提出了使用反射的方法。在三星设备上似乎可以工作,但其他制造商使用了不同的实现方式。 - Pavel Machala
显示剩余2条评论

2

1
如@Hunter建议,您需要使用来自电话管理器的“listen”,但如果它是api 30+,则需要授予READ_PHONE权限并侦听LISTEN_DISPLAY_INFO_CHANGED,并覆盖从PhoneStateListener中的onDisplayInfoChanged
(context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).listen(customPhoneStateListener,PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)

这段文本的英译中文为:

监听器应该像这样:


private  class CustomPhoneStateListener : PhoneStateListener() {
    override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
               
                super.onDisplayInfoChanged(telephonyDisplayInfo)
              
                    when (telephonyDisplayInfo.overrideNetworkType) {
                         //5G
                        OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO,
                        OVERRIDE_NETWORK_TYPE_NR_NSA,
                        OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> setNetworkChange(NETWORK_TYPE_NR)
                        OVERRIDE_NETWORK_TYPE_LTE_CA -> {
                            setNetworkChange(19) //LTE+
                        }
                        else -> setNetworkChange(telephonyDisplayInfo.networkType)
                    }
                } else {
                    setNetworkChange(telephonyDisplayInfo.networkType)
                }
            }

   }

链接: https://developer.android.com/about/versions/11/features/5g#detection


0

我从SM-G977N固件中提取了ServiceState.java文件,并确认他们添加了ServiceState.getNrStatus()

如果NetworkRegistrationState.NR_STATUS_CONNECTED = 3,则表示5G(NR)已激活。


反射技巧自Pie以来已经不再适用,请参见https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces。 - TZW
不对。您仍然可以在公共方法上使用反射。如果在私有/受保护的方法/字段上使用反射,则会遇到麻烦。 - Pavel Machala

0

无法将此作为评论添加,但正如@Pavel Machala所说,在AOSP中查看ServiceState类会产生以下结果:

/**
 * Get the NR 5G status of the mobile data network.
 * @return the NR 5G status.
 * @hide
 */
public @NRStatus int getNrStatus() {
    final NetworkRegistrationState regState = getNetworkRegistrationState(
            NetworkRegistrationState.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
    if (regState == null) return NetworkRegistrationState.NR_STATUS_NONE;
    return regState.getNrStatus();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接