简体   繁体   English

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

[英]Detecting target SimCard of incoming call in Multi-Sim devices

I've read a lot of posts and tried many solutions, but the common point of all posts was that they were all outdated and at least I couldn't find a solution that would work on newer versions of Android.我阅读了很多帖子并尝试了许多解决方案,但所有帖子的共同点是它们都已过时,至少我找不到适用于较新版本的 Android 的解决方案。

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

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

Post 3 , Result:发布 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));
}

Logs:日志:

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

it displays the same logs with the same value 0 for first SimCard and second SimCard.它为第一个 SimCard 和第二个 SimCard 显示具有相同值 0 的相同日志。

I've also tried other similar posts.我也尝试过其他类似的帖子。 None worked on new versions of android!没有人适用于新版本的android!

Is there another solution that works on newer versions of Android (7.0 or higher)?是否有另一种适用于较新版本的 Android(7.0 或更高版本)的解决方案?

If you have done like this it should work.如果你这样做了,它应该可以工作。 make sure your testing device runs on Android 5.1 or above.确保您的测试设备在 Android 5.1 或更高版本上运行。 dual sim support is added in v 5.1 (Check here )在 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
        }
    }
}

Make sure you added the below permission in manifest确保您在清单中添加了以下权限

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

NOTE:笔记:

These values need not to come all the time.这些值不需要一直出现。 network provider support is required.Please read the documentation here需要网络提供商支持。请阅读此处的文档

Carrier id of the current subscription.当前订阅的运营商 ID。 Return UNKNOWN_CARRIER_ID if the subscription is unavailable or the carrier cannot be identified.如果订阅不可用或无法识别运营商,则返回 UNKNOWN_CARRIER_ID。

Officially, the only documented value provided by the intent is the phone number.正式地,意图提供的唯一记录值是电话号码。

Some constructors add other values like sim slot number in the intent, but this is not mandatory.一些构造函数在意图中添加了其他值,例如 sim 插槽号,但这不是强制性的。 This is why there are so many slot keys names possible, like those presented in post 3 , each constructor adding his own implementation.这就是为什么有这么多槽键名称可能的原因,就像帖子 3中介绍的那样,每个构造函数都添加了自己的实现。

It's also possible some constructor didn't add this value in some models, and that's certainly the case on your model.也有可能某些构造函数没有在某些模型中添加此值,您的 model 肯定就是这种情况。 There is no way of finding this value if the constructor don't deliver it.如果构造函数不提供该值,则无法找到该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM