首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何判断双卡安卓设备中哪张SIM卡收到短信

如何判断双卡安卓设备中哪张SIM卡收到短信
EN

Stack Overflow用户
提问于 2016-03-13 17:45:29
回答 6查看 13.6K关注 0票数 10

我正在做一个项目,以同步在Android手机上收到的短信到一个在线数据库。我可以通过调用getOriginatingAddress()方法来获取发件人的电话号码。但我找不到任何解决方案来找出我设备上的哪一个SIM卡收到了这条短信。

我在网上搜索了一下,但是找不到解决这个问题的方法。有没有办法得到短信接收者的号码?

EN

回答 6

Stack Overflow用户

发布于 2016-08-11 20:57:28

我在这个问题上遇到了一些非常困难的问题,最终我找到了一个解决方案,尽管我只在api级别22以上进行了测试。

您必须查看收到的意图中的额外信息。在我的例子中,intent的额外捆绑包中有两个键是有用的:"slot“和"subscription”。

示例如下:

代码语言:javascript
运行
复制
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
                */
            }
}

我没有找到关于捆绑包中密钥的文档,所以这可能取决于设备/制造商,我可以想象密钥是不同的或类似的东西。您可以通过转储捆绑包的密钥集来验证这一点:

代码语言:javascript
运行
复制
Set<string> keyset = bundle.keySet();

编辑:

有关SIM卡的电话号码的信息可能根本不可用,因为如果不存储在SIM卡上,则可能无法查询,但所有其他信息将通过SubscriptionManager获得:

代码语言:javascript
运行
复制
SubscriptionManager manager = SubscriptionManager.from(appContext);
SubscriptionInfo = manager.getActiveSubscriptionInfo(sub);

代码语言:javascript
运行
复制
SubscriptionInfo = manager.getActiveSubscriptionInfoForSimSlotIndex(slot);

SubscriptionInfo中有一些有用的信息,比如电话号码,正如我上面解释的那样,不能保证电话号码是可用的。

我还忘了提一下,android的双SIM卡支持是从api level 22添加的。

票数 22
EN

Stack Overflow用户

发布于 2018-06-18 04:10:14

我使用了Levente Püsök的答案,但我并没有在所有的设备上测试。

代码语言:javascript
运行
复制
try {
Bundle bundle = intent.getExtras();
int slot = -1;
if (bundle != null) {
Set<String> keySet = bundle.keySet();
for(String key:keySet){
  switch (key){
    case "slot":slot = bundle.getInt("slot", -1);
    break;
    case "simId":slot = bundle.getInt("simId", -1);
    break;
    case "simSlot":slot = bundle.getInt("simSlot", -1);
    break;
    case "slot_id":slot = bundle.getInt("slot_id", -1);
    break;
    case "simnum":slot = bundle.getInt("simnum", -1);
    break;
    case "slotId":slot = bundle.getInt("slotId", -1);
    break;
    case "slotIdx":slot = bundle.getInt("slotIdx", -1);
    break;
    default:
      if(key.toLowerCase().contains("slot")|key.toLowerCase().contains("sim")){
       String value = bundle.getString(key, "-1");
       if(value.equals("0")|value.equals("1")|value.equals("2")){
         slot = bundle.getInt(key, -1);
       }
    }


  }
}

 Log.d("slot", "slot=>"+slot);

 }

}catch (Exception e){
Log.d(TAG, "Exception=>"+e);
 }
票数 3
EN

Stack Overflow用户

发布于 2021-05-12 10:30:10

这对我很好,试试可能对你有帮助。

代码语言:javascript
运行
复制
        if (bundle != null) {
            int slot = -1;
            Set<String> keySet = bundle.keySet();
            for(String key:keySet){
                if(key.equals("phone")){slot = bundle.getInt("phone", -1);}//in api 29
                else if(key.equals("slot")){slot = bundle.getInt("slot", -1);}
                else if(key.equals("slotId")){slot = bundle.getInt("slotId", -1);}
                else if(key.equals("slot_id")){slot = bundle.getInt("slot_id", -1);}
                else if(key.equals("slotIdx")){slot = bundle.getInt("slotIdx", -1);}
                else if(key.equals("simId")){slot = bundle.getInt("simId", -1);}
                else if(key.equals("simSlot")){slot = bundle.getInt("simSlot", -1);}
                else if(key.equals("simnum")){slot = bundle.getInt("simnum", -1);}
            }
            System.out.println("  The sim no is >>>  "+slot);

            //or get subscription and do next


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                int sub = bundle.getInt("subscription", -1);
                SubscriptionManager manager = SubscriptionManager.from(context);
                SubscriptionInfo subnfo = manager.getActiveSubscriptionInfo(sub);//this requires READ_PHONE_STATE permission
                System.out.println(
                        "\n The sim no is >>>  "+subnfo.getSimSlotIndex()
                        //or subnfo.getCardId()
                        +"\n The sim no is >>>  "+subnfo.getCardId()//this requires api 29 and +++
                        //some infos may you need it
                        +"\n Phone No is >>>  "+subnfo.getNumber()//not guaranteed to be available
                        +"\n carrier Name is >>>  "+subnfo.getCarrierName()//the operator name
                        +"\n carrier display Name is >>>  "+subnfo.getDisplayName()//that you typed in dual sim settings
                );
            }

        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35968766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档