且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

双卡双待Android手机SIM其接到来电

更新时间:2023-01-06 12:31:29

Android不支持双卡双待的手机,直到是Android 5.1,因此任何扩展,以支持它可能是设备和特定版本。以下是具体的类使用 MultiSimTelephonyManager 的一个变体来处理双SIMS,包括三星的Andr​​oid二重奏4.4.4根据银河J1手机。

Android does not support dual sim phone until Android 5.1 and therefore any extension to support it may be device and version specific. The following is specific for the class of phones using a variant of MultiSimTelephonyManager to handle dual sims, including Samsung duos galaxy J1 under Android 4.4.4.

基本上这个类的双卡双待手机使用 MultiSimTelephonyManager 的两个实例,从常规的 TelephonyManager 并各自负责一个SIM插槽,作为一个接口来控制手机。

Basically this class of dual sim phones use two instances of MultiSimTelephonyManager, subclassed from the regular TelephonyManager and each responsible for one SIM slot, as an interface to control the phone.

之一来检测来电呼叫的装置的是使用 PhoneStateListener 一>类(而不是使用一个接收器),以检测在电话状态改变。在 PhoneStateListener 在这些手机被修改(而不是子类),以包括 mSubscription 字段,它应说明的SIM卡插槽监听器。

One of the means to detect the incoming call is to use the PhoneStateListener class (instead of using a receiver) to detect change in phone states. The PhoneStateListener in these phones are modified (rather than subclassed) to include a mSubscription field which should indicate the SIM slot of the listener.

无论是 MultiSimTelephonyManager 类和 PhoneStateListener $ C $的 mSubscription 字段C>不在标准SDK。要编译应用程序使用这些接口,需要Java反射。

Both the MultiSimTelephonyManager class and the mSubscription field of PhoneStateListener are not in the standard SDK. To compile the app to use these interface, Java Reflection is needed.

以下code应该大致说明了如何可以摆脱来电SIM卡插槽的信息。我没有要测试的设备,所以code可能需要改进。

The following code should roughly illustrate how you could get the sim slot information from incoming calls. I do not have the device to test, so the code may need refinements.

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

Set up the listener during your initialization stage -

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 字段来听电话状态的变化:

Override PhoneStateListener and set the mSubscription field to listen to phone state changes:

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
    }

}

您还需要创建一个名为 MultiSimTelephonyManager.java 在[项目]的文件/ src目录/安卓/电话目录。

You will also need to create a file named MultiSimTelephonyManager.java at the [project]/src/android/telephony directory.

package android.telephony;

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

您或许应该做一些错误检查,特别是检查手机是目标模型,用code时。
请警惕(再次),上面会不会在大多数其他手机和同一个电话的其他Android版本。

You should probably do some error checking and especially check if the phone is the target model, when using the code. Please be warned (again) that the above would not work in most other phones and other Android versions of the same phone.