2

I want to get current Signal Strengths in android device on any button click.
I have written this

public static int getSignal(Context c) {
    class MyPhoneStateListener extends PhoneStateListener {

        int signal;

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            signal = signalStrength.getGsmSignalStrength();
        }
    }
    TelephonyManager Tel;
    MyPhoneStateListener MyListener;
    MyListener = new MyPhoneStateListener();
    Tel = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
    return MyListener.signal;
}

code but it always returns me 0

Please somebody tell me how to get current Signal Strengths.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
kinju
  • 21
  • 2

1 Answers1

2

code but it always return me 0

Of course. onSignalStrengthsChanged() will not have been called yet. And, you stop listening with your LISTEN_NONE line, so you will probably never

Please any body tell me how to get current Signal Strengths.

You will get the signal strength in onSignalStrengthsChanged(), once a change has been detected in the signal strength, if you get rid of the LISTEN_NONE line. Only use LISTEN_NONE when you are done with the listener.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • there's no way to get the current signal strength right now? What if I don't want to wait an undeterminate amount of time to wait for the onSignalStrengthsChanged() to do something ? – Someone Somewhere Jun 17 '11 at 00:50
  • Pls if you find the solution tell me? – Piraba Aug 01 '11 at 12:47
  • Hmm - as detailed [here](http://stackoverflow.com/a/3889407/281545) `after registering PhoneStateListener you will have instant notification`. Still your answer [should be valid](http://stackoverflow.com/questions/3888775/instant-signal-strength/3889407#comment4143475_3890419) – Mr_and_Mrs_D Jul 08 '13 at 17:35