androidcellinfo

Android > How to get service state for both sim cards in dual sim device?


I need it for API 22 and above. I saw that we have telephonyManager.getServiceState - but I don't know how to get it for sim1 and for sim2 exactly. Also we have CellInfo.serviceState - but it's only from API 28.

How to get it? I don't need any listeners, I just want to get service state at the certain time

Please help!


Solution

  • After some researches, implemented this solution:

    @SuppressLint("MissingPermission", "NewApi")
        private fun getServiceState(simSlotNmb: Int): String {
            try {
                val serviceState: ServiceState?
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    serviceState = if (subscriptionManager != null && subscriptionManager!!.activeSubscriptionInfoCount > 1) {
                            val subsId =
                                subscriptionManager!!.getActiveSubscriptionInfoForSimSlotIndex(
                                    simSlotNmb
                                ).subscriptionId
                            val telephonyManager =
                                (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager)
                                    .createForSubscriptionId(subsId)
    
                            telephonyManager.serviceState
                        } else {
                            telephonyManager.serviceState
                        }
                } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && subscriptionManager != null
                          && subscriptionManager!!.activeSubscriptionInfoCount > 1) {
                    val subsId = subscriptionManager!!.getActiveSubscriptionInfoForSimSlotIndex(simSlotNmb).subscriptionId
                    val telephonyManagerForSlot
                            = (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager)
                            .createForSubscriptionId(subsId)
                    telephonyManagerForSlot.listen(phoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE)
                    telephonyManagerForSlot.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
                    serviceState = latestServiceState
                } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && subscriptionManager != null
                    && subscriptionManager!!.activeSubscriptionInfoCount > 1) {
                    val noConnectionDbm = -110
                    val dbm = getSignalDbm(simSlotNmb)
                    serviceState = ServiceState()
                    if(dbm < noConnectionDbm) {
                        serviceState.state = ServiceState.STATE_OUT_OF_SERVICE
                    } else {
                        serviceState.state = ServiceState.STATE_IN_SERVICE
                    }
                } else {
                    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE)
                    telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
                    serviceState = latestServiceState
                }
                return when (serviceState?.state) {
                    ServiceState.STATE_IN_SERVICE -> "in service"
                    ServiceState.STATE_EMERGENCY_ONLY -> "emergency only"
                    else -> "out of service"
                }
            } catch (exc: Exception) {
                exc.printStackTrace()
    
                return when(exc) {
                    is ArrayIndexOutOfBoundsException -> "out of service"
                    else -> Constants.error
                }
            }
        }