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.
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.
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.
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.
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) {
//
}
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
}
}
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);
}
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.