其他分享
首页 > 其他分享> > sim卡接收电话的双卡安卓手机

sim卡接收电话的双卡安卓手机

作者:互联网

我有一个Android应用程序,可以检测来电,我需要改进这个应用程序,以便在二重奏移动设备上工作.
所以我创建了一个在清单中注册的广播接收器用于操作:电话状态已更改,在我的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所以这个方法不起作用,我的意思是哪个sim在所有情况下都返回-1.谁能帮我?

解决方法:

Android在Android 5.1之前不支持双卡手机,因此支持它的任何扩展都可能是设备和版本特定的.以下内容针对使用MultiSimTelephonyManager变体处理双重SIM卡的手机类别,包括Android 4.4.4下的三星duos galaxy J1.

基本上这类双卡手机使用MultiSimTelephonyManager的两个实例,从常规TelephonyManager中分类,每个实例负责一个SIM插槽,作为控制手机的接口.

检测来电的方法之一是使用PhoneStateListener类(而不是使用接收器)来检测电话状态的变化.修改这些电话中的PhoneStateListener(而不是子类)以包括mSubscription字段,该字段应指示监听器的SIM插槽.

MultiSimTelephonyManager类和PhoneStateListener的mSubscription字段都不在标准SDK中.要编译应用程序以使用这些界面,需要Java Reflection.

以下代码应粗略说明如何从传入呼叫中获取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字段以侦听电话状态更改:

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
    }

}

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

package android.telephony;

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

您应该进行一些错误检查,特别是在使用代码时检查手机是否是目标型号.
请注意(再次)上述内容不适用于大多数其他手机和同一部手机的其他Android版本.

标签:telephonymanager,android
来源: https://codeday.me/bug/20190929/1831619.html