How to get LTE signal strength in Android?

前端 未结 4 857
渐次进展
渐次进展 2020-12-02 19:13

I am using Verizon\'s new LTE handset from HTC Thunderbolt. I cannot find the API to query for the signal strength while the handset is on LTE. By entering the field test mo

4条回答
  •  半阙折子戏
    2020-12-02 19:44

    This is complete working implementation of a sample app which uses a reflection to get LTE signal strength:

    import java.lang.reflect.Method;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.telephony.PhoneStateListener;
    import android.telephony.SignalStrength;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    
    public class MainActivity extends Activity
    {
        /*
         * Some significant methods for LTE: getLteSignalStrength, getLteRssi,
         * getLteRsrp and getRsrq. RSRP provides information about signal strength
         * and RSSI helps in determining interference and noise information. RSRQ
         * (Reference Signal Receive Quality) measurement and calculation is based
         * on both RSRP and RSSI.
         */
    
        private SignalStrength      signalStrength;
        private TelephonyManager    telephonyManager;
        private final static String LTE_TAG             = "LTE_Tag";
        private final static String LTE_SIGNAL_STRENGTH = "getLteSignalStrength";
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    
            // Listener for the signal strength.
            final PhoneStateListener mListener = new PhoneStateListener()
            {
                @Override
                public void onSignalStrengthsChanged(SignalStrength sStrength)
                {
                    signalStrength = sStrength;
                    getLTEsignalStrength();
                }
            };
    
            // Register the listener for the telephony manager
            telephonyManager.listen(mListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        }
    
        private void getLTEsignalStrength()
        {
                try
                {
                    Method[] methods = android.telephony.SignalStrength.class.getMethods();
    
                    for (Method mthd : methods)
                    {
                        if (mthd.getName().equals(LTE_SIGNAL_STRENGTH))
                        {
                            int LTEsignalStrength = (Integer) mthd.invoke(signalStrength, new Object[] {});
                            Log.i(LTE_TAG, "signalStrength = " + LTEsignalStrength);
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.e(LTE_TAG, "Exception: " + e.toString());
                }
        }
    }
    

提交回复
热议问题