You know the SIM number only in the broadcast receiver. After one month of research, I got one solution that works great for me, as shown below.
First add the android.permission.READ_PHONE_STATE permission to the manifest file
Implement a receiver for a phone event that accepts call / sms events for your application
public class CallSMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { Log.d("SIM_SLOT"," Slot Number "+capturedSimSlot(extras)); } } public int capturedSimSlot(Bundle bundle){ int whichSIM =-1; if (bundle.containsKey("subscription")) { whichSIM = bundle.getInt("subscription"); } if(whichSIM >=0 && whichSIM < 5){ sim = ""+whichSIM; }else{ if (bundle.containsKey("simId")) { whichSIM = bundle.getInt("simId"); }else if (bundle.containsKey("com.android.phone.extra.slot")) { whichSIM = bundle.getInt("com.android.phone.extra.slot"); }else{ String keyName = ""; for(String key : bundle.keySet()){ if(key.contains("sim")) keyName =key; } if (bundle.containsKey(keyName)) { whichSIM = bundle.getInt(keyName); } } } return whichSIM; } }
android dual-sim
user3131373
source share