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

Detecting target SimCard of incoming call in Multi-Sim devices

我阅读了很多帖子并尝试了很多解决方案,但所有帖子的共同点是它们都已过时,至少我找不到适用于更新版本 Android.

Post 1,结果: intent.getExtras().getInt("simId", -1)总是returns-1

Post 2,结果:intent.getExtras().getInt("slot", -1)总是returns-1

Post 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 的相同日志。

我也试过其他类似的帖子。 None 致力于 android!

的新版本

是否有其他解决方案适用于更新版本的 Android(7.0 或更高版本)?

如果你这样做了它应该工作。确保您的测试设备在 Android 5.1 或更高版本上运行。在 v 5.1 中添加了双 SIM 卡支持(检查 here

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"/>

注意:

这些值不需要一直出现。需要网络提供商支持。请阅读文档 here

Carrier id of the current subscription. Return UNKNOWN_CARRIER_ID if the subscription is unavailable or the carrier cannot be identified.

官方的 the only documented value 意图提供的是 phone 号码。

一些构造函数在意图中添加了其他值,例如 sim 槽号,但这不是强制性的。这就是为什么有这么多可能的插槽键名称,如 post 3 中所示的名称,每个构造函数都添加了自己的实现。

也有可能某些构造函数没有在某些模型中添加此值,在您的模型中肯定是这种情况。如果构造函数不传递它,则无法找到该值。