双卡双待Android手机SIM其接到来电 [英] dual sim android phone which sim receive a call
问题描述
我有一个检测来电Android应用程序,我需要提高这个程序一个二重奏移动设备上运行。
所以我创建清单登记行为的广播接收器:手机状态改变,在我的onReceive方法,我需要检查哪些SIM接听电话。这是我的code
I have an android application that detect the incoming calls, i need to improve this app to work on a duos mobile device. so i create a broadcast receiver registered in manifest for actions: phone state changed and on my onReceive method i need to check which sim receive the call. This is my code
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在所有情况下。谁能帮我?
I run this app on a device 4.2 2 and its working normally but when i run it on a device 4 4.4 so this method does not work, i mean that which sim return -1 in all cases. Can anyone help me?
推荐答案
Android不支持双卡双待的手机,直到是Android 5.1,因此任何扩展,以支持它可能是设备和特定版本。以下是具体的类使用 MultiSimTelephonyManager
的一个变体来处理双SIMS,包括三星的Android二重奏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.
这篇关于双卡双待Android手机SIM其接到来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!