android - 如何知道每次通话/短信的 SimSlot 号码?

标签 android dual-sim

您只在广播接收器中知道 sim 插槽编号。经过一个月的研究,我得到了一个适合我的解决方案,如下所示

首先将 android.permission.READ_PHONE_STATE 权限添加到您的 list 文件

为您的应用程序接收调用/短信事件的电话事件实现接收器

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;
    }
} 

最佳答案

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
  }
 }
}

在联想K3 note中测试

关于android - 如何知道每次通话/短信的 SimSlot 号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34506619/

相关文章:

android - 使用 android.telephony.TelephonyManager.getDeviceId() 时,双卡 android 手机是否总是返回相同的 IMEI

android - 如何使用 SIM2 或双 SIM 卡提交短信

android - 在 Marshmallow 及以下 android 中通过 sim 插槽获取通话记录

android - 轻推分页回收 View 以在不切换的情况下提示下一页

在 Eclipse 中再次安装新软件后 android sdk 损坏

android - 在双卡手机中检测当前/主 SIM

android - 以编程方式在android中检索双SIM卡的IMEI号码

android - 是时候将 Eclipse 升级到版本 3.6.xxx 了吗

java - 三星手机imageView中的照片方向

android - RecyclerView 的空状态项目未显示