如何确定哪个SIM卡在Dual SIM Android设备中接收到短信 [英] How to figure out which SIM received SMS in Dual SIM Android Device

查看:195
本文介绍了如何确定哪个SIM卡在Dual SIM Android设备中接收到短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

尽管这个问题我只在api 22级别以上进行了测试,但我确实遇到了一些困难,终于找到了解决方案.

您必须查看接收到的意图中的其他信息.就我而言,额外的意图捆绑包中有两个键非常有用:广告位"和订阅".

这里是示例:

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

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

Set<string> keyset = bundle.keySet();

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

SubscriptionManager manager = SubscriptionManager.from(appContext);
SubscriptionInfo = manager.getActiveSubscriptionInfo(sub);

SubscriptionInfo = manager.getActiveSubscriptionInfoForSimSlotIndex(slot);

SubscriptionInfo 中有一些有用的信息,例如如上文所述,不能保证可以使用的电话号码.

我也忘了提到从api级别22开始增加了库存android中对Dual-SIM的支持.

I'm working on a project to sync sms received in a Android phone to a online database. I can get sender's number by calling getOriginatingAddress() method. But I can't find any solution to figure out which SIM in my device received the sms.

I searched the web about it, but couldn't find a way to figure this out. Is there any way to get SMS receiver's number?

解决方案

I had some really hard time with this problem and finally I found a solution, although i tested it only above api level 22.

You have to take a look at the extra information in the received intent. In my case there are two keys in the extra Bundle of the intent which are useful: "slot" and "subscription".

Here is the example:

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

I did not find documentation about the keys in the Bundle so this could be device/manufacturer dependent, i can imagine that the keys are different or something like that. You can verify this by dumping the key set of the bundle:

Set<string> keyset = bundle.keySet();

EDIT:

The information about the phone number of the SIM card may not be available at all since if it is not stored on the SIM card it is likely cannot be queried, but all other information will be available through the SubscriptionManager:

SubscriptionManager manager = SubscriptionManager.from(appContext);
SubscriptionInfo = manager.getActiveSubscriptionInfo(sub);

or

SubscriptionInfo = manager.getActiveSubscriptionInfoForSimSlotIndex(slot);

And there are some useful info in the SubscriptionInfo such as the phone number which one is not guaranteed to be available as I explained above.

I also forgot to mention that the Dual-SIM support in stock android was added from api level 22.

这篇关于如何确定哪个SIM卡在Dual SIM Android设备中接收到短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆