How to get a PhoneStateListener when using Dual SIM functionality

心已入冬 提交于 2019-12-04 15:01:12

I make simple ArrayList with Listeners and it's work for me fine (it's Kotlin btw)

In my activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.sim_selector)
    checkForForwarding()
}

fun getSimsCount(): Int {
    val subscriptionManager = SubscriptionManager.from(this)
    val activeSubscriptionInfoList = subscriptionManager.activeSubscriptionInfoList
    return activeSubscriptionInfoList.size
}

class SimForwardListeners{
    var subscriptionId: Int = 0
    lateinit var manager: TelephonyManager
    lateinit var phoneStateListener: MyPhoneStateListener
}

private val simsForwardListeners: ArrayList<SimForwardListeners> = arrayListOf()

fun checkForForwarding() {
    val subscriptionManager = getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager

    for (slotIndex in 0 until getSimsCount()) {
        val z = SimForwardListeners()
        z.phoneStateListener = MyPhoneStateListener(slotIndex)
        z.phoneStateListener.setView(this)
        z.subscriptionId = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(slotIndex).subscriptionId
        z.manager = (getSystemService(TELEPHONY_SERVICE) as TelephonyManager).createForSubscriptionId(z.subscriptionId)
        z.manager.listen(z.phoneStateListener, PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR)
        simsForwardListeners.add(z)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!