How to get a PhoneStateListener when using Dual SIM functionality
Mobile Technologies
Mobile Computing
2 years ago
5
Star Rating
1
Rating
_x000D_
_x000D_
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:
I create a TelephonyManager object:
val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
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!")
}
}
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:
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
I create a separate TelephonyManager for the second SIM card, with the correct subscriptionId:
val secondTelephonyManager = (getSystemService(TELEPHONY_SERVICE) as TelephonyManager).createForSubscriptionId(subscriptionIdOfSimCard2)
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, i.e. 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?
Posted on 16 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Take Quiz To Earn Credits!
Turn Your Knowledge into Earnings.