Background
I'm working on an app that implements InCallService, so it can handle phone calls
The problem
On devices with multi-SIM, I need to show information of which SIM and associated phone number is used (of the current device).
Thing is, I can't find where to get this information.
What I've found
Given that I reach a function like onCallAdded, I get an instance of Call class, so I need to associate something I get from there, to a sim slot and phone number.
Using
call.getDetails().getHandle(), I can get a Uri consisting only the phone number of the other person that called (who called the current user, or who the current user has called).I can iterate over all SIM cards, but can't find what I can use to map between them and the current call:
final TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
for (final SubscriptionInfo subscriptionInfo : subscriptionManager.getActiveSubscriptionInfoList()) {
final TelephonyManager subscriptionId = telephonyManager.createForSubscriptionId(subscriptionInfo.getSubscriptionId());
}
There was an old code that doesn't work anymore that uses
call.getDetails().getAccountHandle().getId()andSubscriptionManager.from(context)getActiveSubscriptionInfoList().getActiveSubscriptionInfoList().I think I can use
telephonyManager.getSubscriptionId(callDetails.getAccountHandle()), but it requires API 30, which is quite new...
The questions
Given a phone call that I get from this callback (and probably others), how can I get the associated SIM slot and phone number of it?
I prefer to know how to do it for as wide range of Android versions as possible, because InCallService is from API 23... It should be possible before API 30, right?
Use
call.getDetails().getAccountHandle()to get PhoneAccountHandle.Then use TelecomManager.getPhoneAccount() to get PhoneAccount instance.
Permission
Manifest.permission.READ_PHONE_NUMBERSis needed for applications targeting API level 31+.Disclaimer: I am no Android expert, so please do validate my thoughts.
EDIT: So solution for both before API 30 and from API 30 could be as such: