简体   繁体   中英

How to get a PhoneStateListener when using Dual SIM functionality

so I am currently implementing a call forwarding feature in Android, for dual SIM devices. In order to read the current state of the call forwarding (enabled/disabled) for a SIM card, I do the following:

  1. I create a TelephonyManager object:

val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager

  1. I create a PhoneStateListener object and override the onCallForwardingIndicatorChanged method:

     val myPhoneStateListener = object: PhoneStateListener() { override fun onCallForwardingIndicatorChanged(isCallForwardingEnabled: Boolean) { if(isCallForwardingEnabled) println("Call forwarding enabled!") else println("Call forwarding disabled!") } } 
  2. I registered the PhoneStateListener :

telephonyManager.listen(myPhoneStateListener, LISTEN_CALL_FORWARDING_INDICATOR)

This works perfectly fine for the primary (the first) SIM card.

But I am having trouble doing the same for the second SIM card. Here is how I am trying to do it:

  1. I use a SubscriptionManager object to retrieve the subscriptionId of the second SIM card:

     val subscriptionManager = getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager val subscriptionIdOfSimCard2 = subscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1).subscriptionId 
  2. I create a separate TelephonyManager for the second SIM card, with the correct subscriptionId:

     val secondTelephonyManager = (getSystemService(TELEPHONY_SERVICE) as TelephonyManager).createForSubscriptionId(subscriptionIdOfSimCard2) 
  3. I create a second PhoneStateListener , just like the one for the first SIM card, lets call it mySecondPhoneStateListener and register it with the second TelephonyManager :

     secondTelephonyManager.listen(mySecondPhoneStateListener, LISTEN_CALL_FORWARDING_INDICATOR) 

The problem now is, that in mySecondPhoneStateListener I don't get callbacks for the second SIM card, but still the primary, first one. After digging around in the Android source code, I found out why that is: In the listen(PhoneStateListener listener, int events) method of the TelephonyManager the wrong subscriptionId is used, ie not the one set in the TelephonyManager but the one in the PhoneStateListener object, which is the subscriptionId of the first SIM card, by default:

public void listen(PhoneStateListener listener, int events) {
    if (mContext == null) return;
    try {
        Boolean notifyNow = (getITelephony() != null);
        sRegistry.listenForSubscriber(listener.mSubId, */ HERE: listener.mSubId is used instead of this.mSubId */
            getOpPackageName(), listener.callback, events, notifyNow);
    } catch (RemoteException ex) {
        // system process dead
    } catch (NullPointerException ex) {
        // system process dead
    }
}

This problem could be solved by setting the correct subscriptionId for the PhoneStateListener object, however the appropriate constructor is hidden:

/**
 * Create a PhoneStateListener for the Phone using the specified subscription.
 * This class requires Looper.myLooper() not return null. To supply your
 * own non-null Looper use PhoneStateListener(int subId, Looper looper) below.
 * @hide */<-- HIDDEN, NOT ACCESSIBLE*/
 */
public PhoneStateListener(int subId) {
    this(subId, Looper.myLooper());
} 

I was able to "solve" this with reflection, by setting the mSubId field of the PhoneStateListener object to the appropriate subscriptionId of the second SIM card.

But there has to be a better way to do this, am I missing something?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM