Domanda

I have a dual sim android phone. I am using a custom broadcast receiver which reads incoming messages with no problem. I wonder if there is a way to find out which sim received the message.

È stato utile?

Soluzione

You can get the active sim's info by using TelephonyManager. FOr example, it's serial number.

TelephonyManager telephoneMgr = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

String simSerial = telephoneMgr.getSimSerialNumber();

You could also get the line1Number, if your network operator has put your number in there, you could compare it to the number you got on the to> field in the SMS message.

String phoneNumber = telephoneMgr.getLine1Number();

Hope it helps

Altri suggerimenti

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 this so this could be device/manufacturer dependent, i can imagine that the keys are diferent or something like that. You can verify this by dumping the key set of the bundle:

Set<string> keyset = bundle.keySet();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top