在Multi-Sim设备中检测来电的目标SimCard


10

我读了很多文章并尝试了许多解决方案,但是所有文章的共同点是它们都已过时,至少我找不到能在较新版本的Android上运行的解决方案。

发布1,结果: intent.getExtras().getInt("simId", -1)总是返回-1

发布2,结果:intent.getExtras().getInt("slot", -1)总是返回-1

发表3,结果:

String[] array = new String[]{
        "extra_asus_dial_use_dualsim",
        "com.android.phone.extra.slot",
        "slot",
        "simslot",
        "sim_slot",
        "subscription",
        "Subscription",
        "phone",
        "com.android.phone.DialingMode",
        "simSlot",
        "slot_id",
        "simId",
        "simnum",
        "phone_type",
        "slotId",
        "slotIdx"
};

for (String item :
        array) {
    Log.i(TAG, "Sim Card - " + item + " -----> " + intent.getExtras().getInt(item));
}

日志:

PhoneCallReceiver: Sim Card - extra_asus_dial_use_dualsim -----> 0
PhoneCallReceiver: Sim Card - com.android.phone.extra.slot -----> 0
PhoneCallReceiver: Sim Card - slot -----> 0
PhoneCallReceiver: Sim Card - simslot -----> 0
PhoneCallReceiver: Sim Card - sim_slot -----> 0
PhoneCallReceiver: Sim Card - subscription -----> 0
PhoneCallReceiver: Sim Card - Subscription -----> 0
PhoneCallReceiver: Sim Card - phone -----> 0
PhoneCallReceiver: Sim Card - com.android.phone.DialingMode -----> 0
PhoneCallReceiver: Sim Card - simSlot -----> 0
PhoneCallReceiver: Sim Card - slot_id -----> 0
PhoneCallReceiver: Sim Card - simId -----> 0
PhoneCallReceiver: Sim Card - simnum -----> 0
PhoneCallReceiver: Sim Card - phone_type -----> 0
PhoneCallReceiver: Sim Card - slotId -----> 0
PhoneCallReceiver: Sim Card - slotIdx -----> 0

对于第一个SimCard和第二个SimCard,将显示相同的日志,且其值均为0。

我也尝试过其他类似的帖子。没有适用于新版本的android!

在较新版本的Android(7.0或更高版本)上,还有其他解决方案吗?


intent.getExtras().getInt("item")<-您不是要使用item而不是"item"吗?
— 迈克尔

此外,你有没有试过打印所有的意图演员确实包含,看是否有在命名相比,你承担什么区别?
— 迈克尔

无论是带有“ item”还是带有item,它总是返回0。
— Alireza Noorali

好吧,"item"在这种情况下几乎可以肯定是不正确的。就像我说的,尝试打印您所拥有的而不是期望的。尽管整个解决方案看起来有些破烂,但是您发现的任何可用密钥在某些设备上可能仍然无法使用。
— 迈克尔

Answers:


1

正式地,意图提供的唯一记录的值是电话号码。

一些构造函数会在意图中添加其他值,例如sim插槽号,但这不是强制性的。这就是为什么插槽键名称如此之多的原因,如第3部分中介绍的那样,每个构造函数都添加了自己的实现。

也有可能某些构造函数未在某些模型中添加此值,并且在模型上肯定是这种情况。如果构造函数不提供该值,则无法找到该值。


因为答案仅是一种解释,所以我不能最终批准或拒绝它。但事实证明,答案是合乎逻辑的。
— Alireza Noorali

2

如果您这样做就可以了。确保您的测试设备在Android 5.1或更高版本上运行。v 5.1中添加了对双SIM卡的支持(请在此处检查)

public class IncomingCallInterceptor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    String callingSIM = "";
    Bundle bundle = intent.getExtras();
    callingSIM = String.valueOf(bundle.getInt("simId", -1));

    if(callingSIM.equals("0")){
           // Incoming call from SIM1
        } else if(callingSIM.equals("1")){
           // Incoming call from SIM2
        }
    }
}

确保您在清单中添加了以下权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

注意 :

这些值不需要一直存在。需要网络提供商支持。请在此处阅读文档

当前订阅的运营商ID。如果订阅不可用或无法识别运营商,则返回UNKNOWN_CARRIER_ID。


1
我已经添加了READ_PHONE_STATE权限,但我始终会得到callingSIM = -1
— Alireza Noorali

@AlirezaNoorali您正在使用哪个设备进行测试。并且您可以在Android 7设备以下运行它?
— droidev

该设备是Samsung Galaxy S6 Android 7.0。我不确定以下android 7设备
— Alireza Noorali

您能够正常检索simId吗?当您不在通话中时
— droidev
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.