Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

telephonymanager - dual sim android phone which sim receive a call

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);
     }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...