I'm working on a project to sync sms received in a Android phone to a online database. I can get sender's number by calling getOriginatingAddress()
method. But I can't find any solution to figure out which SIM in my device received the sms.
I searched the web about it, but couldn't find a way to figure this out. Is there any way to get SMS receiver's number?
I had some really hard time with this problem and finally I found a solution, although i tested it only above api level 22.
You have to take a look at the extra information in the received intent. In my case there are two keys in the extra Bundle of the intent which are useful: "slot" and "subscription".
Here is the example:
public class IncomingSms extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
Bundle bundle = intent.getExtras();
int slot = bundle.getInt("slot", -1);
int sub = bundle.getInt("subscription", -1);
/*
Handle the sim info
*/
}
}
I did not find documentation about the keys in the Bundle so this could be device/manufacturer dependent, i can imagine that the keys are different or something like that. You can verify this by dumping the key set of the bundle:
Set<string> keyset = bundle.keySet();
EDIT:
The information about the phone number of the SIM card may not be available at all since if it is not stored on the SIM card it is likely cannot be queried, but all other information will be available through the SubscriptionManager:
SubscriptionManager manager = SubscriptionManager.from(appContext);
SubscriptionInfo = manager.getActiveSubscriptionInfo(sub);
or
SubscriptionInfo = manager.getActiveSubscriptionInfoForSimSlotIndex(slot);
And there are some useful info in the SubscriptionInfo such as the phone number which one is not guaranteed to be available as I explained above.
I also forgot to mention that the Dual-SIM support in stock android was added from api level 22.