How to detect Samsung S10 5G is running on 5G network?

前端 未结 5 756
一整个雨季
一整个雨季 2021-01-07 01:24

Android Q added a new network type, NETWORK_TYPE_NR, for 5G, it is not available for Android Pie. Yet the just released Samsung S10 5G has fully supported 5G function, it ca

5条回答
  •  鱼传尺愫
    2021-01-07 01:46

    as @Hunter suggested you need to use "listen" from telephony manager but if it's api 30+ you need to have READ_PHONE permission granted and listen for LISTEN_DISPLAY_INFO_CHANGED and override onDisplayInfoChanged from PhoneStateListener

    (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).listen(customPhoneStateListener,PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
    

    The listener should be something like this:

    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)
                    }
                }
    
       }
    

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

提交回复
热议问题