Android 双sim安卓手机,哪个sim卡可以接听电话
我有一个android应用程序可以检测来电,我需要改进这个应用程序,使其能够在duos移动设备上工作。 因此,我创建了一个在清单中注册的广播接收器,用于操作:电话状态已更改,在onReceive方法中,我需要检查哪个sim卡接收呼叫。这是我的密码Android 双sim安卓手机,哪个sim卡可以接听电话,android,telephonymanager,Android,Telephonymanager,我有一个android应用程序可以检测来电,我需要改进这个应用程序,使其能够在duos移动设备上工作。 因此,我创建了一个在清单中注册的广播接收器,用于操作:电话状态已更改,在onReceive方法中,我需要检查哪个sim卡接收呼叫。这是我的密码 Protected void onReceive(Context c, Intent i) { Int whichSim = intent getIntExtra("simSlot",-1); // so
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。有人能帮我吗?建议
“com.android.phone.extra.slot”
可能也适用于某些手机。也许可以同时尝试这两种方法,并使用未返回-1
?的方法。在Android 5.1之前,Android不支持双sim手机,因此支持双sim手机的任何扩展可能是特定于设备和版本的。以下是使用MultiSimTelephonyManager
变体来处理双SIM卡的特定手机类别,包括Android 4.4.4下的三星duos galaxy J1
基本上,这类双sim手机使用两个MultiSimTelephonyManager
实例作为控制手机的接口,该实例从常规的子类中分类,每个实例负责一个sim插槽
检测传入呼叫的方法之一是使用类(而不是使用接收器)来检测电话状态的变化。这些手机中的PhoneStateListener
被修改(而不是子类化)以包含mSubscription
字段,该字段应指示监听器的SIM卡插槽
标准SDK中不包含PhoneStateListener
的MultiSimTelephonyManager
类和mSubscription
字段。要编译应用程序以使用这些接口,需要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) {
//
}
您还需要在[project]/src/android/telephony目录下创建一个名为MultiSimTelephonyManager.java
的文件
package android.telephony;
public interface MultiSimTelephonyManager {
public void listen(PhoneStateListener listener,int events);
}
在使用代码时,您可能应该进行一些错误检查,特别是检查手机是否是目标型号。
请(再次)注意,上述内容在大多数其他手机和同一手机的其他安卓版本中不起作用 您尝试过这种方法吗:- 在这种方法中,1个Sim卡的值为0,第二个Sim卡的值为1。 //4.4手机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();
}
我也用这个,不起作用。。我想除了simslot之外,双sim卡还有一个新名称。你是否尝试过转储intent的所有附加功能
intent.getExtras()
?这里没有有用的东西吗?intent.getExtras()只返回传入号码和状态(铃声或空闲),但不返回sim ID。您的设备是哪些品牌/型号?三星duos galaxy J,SM-J100Handroid版本4.4.4Hi,我已尝试实现您的解决方案。现在,logcat中有很多错误。我在此线程中创建了一篇帖子(我的日志太长,无法发表评论)。请看。正如我所说,解决方案是针对特定android版本下的特定设备,而不是针对双sim手机的通用解决方案。如果未经检查就在其他设备/版本中按原样使用代码,则最终肯定会出现不同的异常。
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();
}