Android > 如何在双 SIM 设备中获取两张 SIM 卡的服务状态?
Android > How to get service state for both sim cards in dual sim device?
我需要 API 22 岁及以上。
我看到我们有 telephonyManager.getServiceState
- 但我不知道如何为 sim1 和 sim2 准确获取它。
我们还有 CellInfo.serviceState - 但它仅来自 API 28.
如何获得?我不需要任何监听器,我只想获取特定时间的服务状态
请帮忙!
经过一番研究,实施了这个解决方案:
@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
}
}
}
我需要 API 22 岁及以上。
我看到我们有 telephonyManager.getServiceState
- 但我不知道如何为 sim1 和 sim2 准确获取它。
我们还有 CellInfo.serviceState - 但它仅来自 API 28.
如何获得?我不需要任何监听器,我只想获取特定时间的服务状态
请帮忙!
经过一番研究,实施了这个解决方案:
@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
}
}
}