Как узнать номер SimSlot для каждого звонка / смс?

Вы знаете номер сим-слота только в широковещательном приемнике. После одного месяца исследований я получил одно решение, которое отлично работает для меня, как показано ниже

Сначала добавьте разрешение android.permission.READ_PHONE_STATE в файл манифеста.

Реализовать приемник для события телефона, который получает события вызова / смс для вашего приложения

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 ответ

Для смс в леденце 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
  }
 }
}

протестировано в Lenovo K3 note

Другие вопросы по тегам