How to know SimSlot Number for every Call / sms?

不打扰是莪最后的温柔 提交于 2019-12-30 08:28:13

问题


You know the sim slot number only in broadcast receiver. After one month of research I got one solution which is work fine for me as below

Firstly Add the android.permission.READ_PHONE_STATE permission to your manifest file

Implement receiver for the phone event which receive 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));
    }
  }

/*below methods captures the sim slot number from bundle */

public int capturedSimSlot(Bundle bundle){

        int whichSIM =-1;
        if (bundle.containsKey("subscription")) {
            whichSIM = bundle.getInt("subscription");
        }
        if(whichSIM >=0 && whichSIM < 5){
            /*In some device Subscription id is return as subscriber id*/
            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;
    }
} 

回答1:


for sms in lollipop 22+

public class MessageReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {  
  int slot = Integer.parseInt((String) intent.getExtras().get("slot"));
  if(slot == 0){
    // sim1
  }
  if(slot == 1){
    // sim2
  }
 }
}

tested in Lenovo K3 note



来源:https://stackoverflow.com/questions/34506619/how-to-know-simslot-number-for-every-call-sms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!