双 sim android phone 哪个 sim 接听电话

dual sim android phone which sim receive a call

我有一个 android 应用程序可以检测来电,我需要改进这个应用程序才能在 duos 移动设备上运行。 所以我创建了一个在清单中注册的广播接收器以进行操作:phone 状态已更改,并且在我的 onReceive 方法上我需要检查哪个 sim 卡接收到呼叫。这是我的代码

   Protected void onReceive(Context c, Intent i)
   {
     Int whichSim = intent
      getIntExtra("simSlot",-1);
      // so this methof return 0            for sim 1 and 1 for sim2
     If(whichSim==-1)
    WhichSim=intent.getIntExtra("com.androie.phone.extra.slot",-1);
     }

我运行 4.2 设备上的这个应用程序 2 并且它正常工作但是当我 运行 它在设备上时 4 4.4 所以这个方法不起作用,我的意思是 which sim return -1 在所有情况下。谁能帮帮我?

This question 建议 "com.android.phone.extra.slot" 也可以在某些手机上使用。也许两者都试一下,用那个没有 return -1?

Android 在 Android 5.1 之前不支持双卡 phone,因此任何支持它的扩展都可能是特定于设备和版本的。以下是针对 phone 的 class 使用 MultiSimTelephonyManager 的变体来处理双模拟,包括 Android 4.4.4 下的 Samsung duos galaxy J1。

基本上这个 class 双 sim phone 使用两个 MultiSimTelephonyManager 实例,从常规 TelephonyManager 中提取 class,每个实例负责一个SIM卡槽,作为控制phone的接口。

检测来电的方法之一是使用PhoneStateListener class(而不是使用接收器)来检测phone状态的变化。这些 phone 中的 PhoneStateListener 被修改(而不是 subclassed)以包含一个 mSubscription 字段,该字段应指示侦听器的 SIM 插槽。

MultiSimTelephonyManagerclass和PhoneStateListenermSubscription字段均不在标准SDK中。要编译应用程序以使用这些接口,需要 Java 反射。

下面的代码应该大致说明了如何从来电中获取 sim 插槽信息。我没有测试设备,所以代码可能需要改进。

在初始化阶段设置监听器 -

try {
    final Class<?> tmClass = Class.forName("android.telephony.MultiSimTelephonyManager");
    // MultiSimTelephonyManager Class found
    // getDefault() gets the manager instances for specific slots
    Method methodDefault = tmClass.getDeclaredMethod("getDefault", int.class);
    methodDefault.setAccessible(true);
    try {
        for (int slot = 0; slot < 2; slot++) {
            MultiSimTelephonyManager telephonyManagerMultiSim = (MultiSimTelephonyManager)methodDefault.invoke(null, slot);     
            telephonyManagerMultiSim.listen(new MultiSimListener(slot), PhoneStateListener.LISTEN_CALL_STATE);
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        // (Not tested) the getDefault method might cause the exception if there is only 1 slot
    }
} catch (ClassNotFoundException e) {
    // 
} catch (NoSuchMethodException e) {
    //
} catch (IllegalAccessException e) {
    //
} catch (InvocationTargetException e) {
    //
} catch (ClassCastException e) {
    //
}

覆盖 PhoneStateListener 并设置 mSubscription 字段以监听 phone 状态变化:

public class MultiSimListener extends PhoneStateListener {

    private Field subscriptionField;
    private int simSlot = -1;

    public MultiSimListener (int simSlot) {
        super();            
        try {
            // Get the protected field mSubscription of PhoneStateListener and set it 
            subscriptionField = this.getClass().getSuperclass().getDeclaredField("mSubscription");
            subscriptionField.setAccessible(true);
            subscriptionField.set(this, simSlot);
            this.simSlot = simSlot; 
        } catch (NoSuchFieldException e) {

        } catch (IllegalAccessException e) {

        } catch (IllegalArgumentException e) {

        }
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // Handle the event here, with state, incomingNumber and simSlot
    }

}

您还需要在 [项目]/src/android/telephony 目录中创建一个名为 MultiSimTelephonyManager.java 的文件。

package android.telephony;

public interface MultiSimTelephonyManager {
    public void listen(PhoneStateListener listener,int events);
}

您可能应该做一些错误检查,尤其是在使用代码时检查 phone 是否是目标模型。 请(再次)警告,以上内容在大多数其他 phone 和相同 phone 的其他 Android 版本中不起作用。

你试过这个方法了吗:- 在这种方法中,您将获得 1 个 Sim 的 0,而对于第二个 Sim,您将获得 1。 //4.4 Phones KitKat 的工作代码

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        whichSIM = intent.getExtras().getInt("subscription");


        if (whichSIM == 0) {
            whichSIM = 1;
            editor.putLong("ChooseSim", whichSIM);
            editor.commit();
            // Incoming call for SIM1

        } else if (whichSIM == 1) {
            whichSIM = 2;
            editor.putLong("ChooseSim", whichSIM);
            editor.commit();

        }