检测哪个SIM卡来电来自Android?

uyhoqukh  于 2023-04-28  发布在  Android
关注(0)|答案(2)|浏览(136)

我需要在多个SIM设备上检测来电的目标SIM卡.我已经厌倦了很多帖子,并尝试了许多解决方案,但所有的解决方案都过时了.不适用于Android的新版本.
我尝试了下面的解决方案,没有任何帮助

int simSlot = intent.getIntExtra("simSlot", -1);
    int simId = intent.getIntExtra("simId", -1);
    int slot = intent.getIntExtra("slot", -1);

全部都是returns -1

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));
}

输出始终为returns 0
一些文章显示这是不可能的。但TrueCaller显示所有android手机中所有来电的目标sim卡。
我想知道这样的解决方案

我试了这么多方法没有帮助.任何帮助将高度appriciated.提前感谢

bxgwgixi

bxgwgixi1#

您可以尝试在BroadcastReceiver类中执行此操作,

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 == "0"){
        // Incoming call from SIM1
    }
    else if(callingSIM =="1"){
        // Incoming call from SIM2
    }
    }
qzlgjiam

qzlgjiam2#

您可以使用SubscriptionInfo类尝试以下解决方案。

  • 但这仅在电话号码与SIM卡相关联时才有效。
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());

 // Create a PhoneStateListener to detect incoming calls
 PhoneStateListener phoneStateListener = new PhoneStateListener() {
     @Override
     public void onCallStateChanged(int state, String phoneNumber) {
         if (state == TelephonyManager.CALL_STATE_RINGING) {
             // Retrieve the list of active subscriptions
             List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
             if (subscriptionInfoList != null) {
                 // Loop through the list of active subscriptions
                 for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
                     // Retrieve the phone number of the SIM card
                     String simPhoneNumber = telephonyManager.getLine1Number(subscriptionInfo.getSubscriptionId());
                     if (simPhoneNumber != null && simPhoneNumber.equals(phoneNumber)) {
                         // The incoming call belongs to this SIM card
                         int simSlotIndex = subscriptionInfo.getSimSlotIndex();
                         Log.d(TAG, "Incoming call belongs to SIM card in slot " + simSlotIndex);
                         break;
                     }
                 }
             }
         }
     }
 };

注册PhoneStateListener,

telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

相关问题