Search code examples
androidtelephonymanagerdual-sim

Android signal strength


Is there any method to get signal strength on both sim cards. I search a lot but I couldn't find any solution. Maybe is there any method to register receiver on second sim card ? I'm working on Android 5.0 and I know that on this version Android officially does not support dual sim solutions. I found only this which nearly fits to me: Check whether the phone is dual SIM Android dual SIM signal strength

Second link presents some way but I cannot use it because method TelephonyManager.listenGemini is not available

Any help ?


Solution

  • Please note: the following is specific to some Android 5.0 devices. It uses hidden interface in Android 5.0, and will not work in earlier AND later versions. In particular, the subscription id changed from long to int when the API is exposed in API 22 (for which you should use the official API anyway).

    For Android 5.0 on HTC M8, you may try the following to get the signal strength of both sim cards:

    Overriding PhoneStateListener and its protected internal variable long mSubId. Since the protected variable is hidden you will need to use reflection.

    public class MultiSimListener extends PhoneStateListener {
    
        private Field subIdField;
        private long subId = -1;
    
        public MultiSimListener (long subId) {
            super();            
            try {
                // Get the protected field mSubId of PhoneStateListener and set it 
                subIdField = this.getClass().getSuperclass().getDeclaredField("mSubId");
                subscriptionField.setAccessible(true);
                subscriptionField.set(this, subId);
                this.subId = subId; 
            } catch (NoSuchFieldException e) {
    
            } catch (IllegalAccessException e) {
    
            } catch (IllegalArgumentException e) {
    
            }
        }
    
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            // Handle the event here, subId indicates the subscription id if > 0
        }
    
    }
    

    You also need to get the list of active subscription IDs from the SubscriptionManager for instantiating the class. Again SubscriptionManager is hidden in 5.0.

    final Class<?> tmClassSM = Class.forName("android.telephony.SubscriptionManager");
    // Static method to return list of active subids
    Method methodGetSubIdList = tmClassSM.getDeclaredMethod("getActiveSubIdList");
    long[] subIdList = (long[])methodGetSubIdList.invoke(null);
    

    Then you can iterate through the subIdList to create instances of MultiSimListener. e.g.

    MultiSimListener listener[subIdList[i]] = new MultiSimListener(subIdList[i]);
    

    You can then call TelephonyManager.listen as usual, for each of the listeners.

    You will need to add error and Android version / device check to the code, as it works only on specific devices / version.