dual sim android phone which sim receive a call

后端 未结 3 553
北海茫月
北海茫月 2020-12-11 08:40

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 mani

相关标签:
3条回答
  • 2020-12-11 09:33

    Have you Try this Method :- in this method you will get 0 for 1 Sim and for second sim you will get 1. //Working code For 4.4 Phones 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();
    
            }
    
    0 讨论(0)
  • 2020-12-11 09:34

    This question suggests "com.android.phone.extra.slot" may also work on some phones. Maybe try both and use the one that didn't return -1?

    0 讨论(0)
  • 2020-12-11 09:37

    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.

    0 讨论(0)
提交回复
热议问题