1

Why we have to set up a listener in order to get signal strength of single current cell when in 3g mode?

We get signal strengths of neighbouringcell using NeghbouringcellInfo class?

Why cannot we get signal strength of current cell like that?

Calvin
  • 617
  • 1
  • 12
  • 34

4 Answers4

7

try the following code:

    ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();


    if (is3g) {
        myListener = new MyPhoneStateListener();
        TelephonyManager telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telManager.listen(myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        public int singalStenths = 0; 
        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength){
            super.onSignalStrengthsChanged(signalStrength);
            int singalStrength  = signalStrength.getGsmSignalStrength();
            singalStenths = signalStrength.getGsmSignalStrength();
            System.out.println("----- gsm strength" + singalStrength );
            System.out.println("----- gsm strength" + singalStenths );

            if (singalStenths > 30) {
                signalstrength.setText("Signal Str : Good");
                signalstrength.setTextColor(getResources().getColor(R.color.good));
            }
            else if(singalStenths > 20 && singalStenths < 30) {
                signalstrength.setText("Signal Str : Average");
                 signalstrength.setTextColor(getResources().getColor(R.color.average));
            }
            else if(singalStenths < 20) {
                signalstrength.setText("Signal Str : Weak");
                 signalstrength.setTextColor(getResources().getColor(R.color.weak));
            }
        }
    };
linquize
  • 19,828
  • 10
  • 59
  • 83
G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
3

the implementation is such that we have to implement listener to get signal strength of current cell, i have wrote the listener and stopped the listener at first read and then kept in variable. no any other way

Calvin
  • 617
  • 1
  • 12
  • 34
  • Not a good enough way. I start my app and sometimes I don't receive a signal strength for ages. – soger Feb 05 '19 at 12:09
3

I tried to use the "Hazmat" code, but unfortunately does not work on my device (Samsung S3 mini Android 4.1.2). It seems that some phones from Samsung have problems with this feature and nobody from Samsung cares. Also other Samsung models have similar problem:

Samsung Galaxy SI -> no problems

Samsung Galaxy SII -> no strength and no refresh

Samsung Galaxy Tab 10.1 -> no strength and no refresh

Samsung Galaxy Note -> no strength and no refresh

(Android Versions: 2.3.3, 2.3.4, 2.3.5, 3.1)

Also reported here and here.

Conclusion: Be aware that a lot of Samsung devices don´t work properly with this function! I am not aware of any workaround for it. Samsung makes our apps unreliable.

Michal
  • 2,071
  • 19
  • 30
0

If I understand well, you don't want to set up a listenner but just get the signal strength whenever you need it. If it's the case here is how to do it:

android.telephony.SignalStrength

You would get the 3G or CDMA or whatever type of cell you are connected to.

hope it helps.

-------- EDIT after comments --------------

The listener is just a nice way to be informed that there is a change in what you are interested in without having to constantly poll it. It's much more efficient.

Here is an example I took from Android source code.

public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
            String updateMsg = "cdma dBM=" + signalStrength.getCdmaDbm();
            mTrackerData.writeEntry(SIGNAL_PROVIDER_TAG, updateMsg);
        } else if  (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
            String updateMsg = "gsm signal=" + signalStrength.getGsmSignalStrength();
            mTrackerData.writeEntry(SIGNAL_PROVIDER_TAG, updateMsg);
        }
    }

This is taken from frameworks/base/tests/LocationTracker/src/com/android/locationtracker/TrackerService.java. Yes this method is called by a listener but it doesn't have to. it's just a much more efficient way to do things than constantly polling. On the other hand, if you just want the signal strength at a specific time, just one shot, nothing prevents you from just calling it from somewhere else.

Also, I suggest you download your Android distribution. With "grep", it gives a lot of examples on how classes, methods etc are used. I'm learning a lot from it. These examples are good since they have been coded by the people who wrote the OS. Also, you can get the source code at Android source code

Volo
  • 28,673
  • 12
  • 97
  • 125
Sylvain Huard
  • 1,378
  • 6
  • 18
  • 29
  • What i want to do is to use method like getGsmSignalStrength(), i do not get anywhere the use of this method without listener. – Calvin Oct 02 '12 at 05:36
  • There is already a default listener to update the little signal strength icon on top of the screen but, you don't need that. There is a structure in the Android internal API that is maintained by the RIL. The RIL constantly monitors the GSM radio and update that structure with lots of info including signal strength. When it changes it will call the listeners but you don't need a listener to get the value. You just call GetGsmSignalStrength and you should get the value from the structure. If you have Android source code, look in SignalStrength.java, you will see. – Sylvain Huard Oct 02 '12 at 13:31
  • in fact look in the source code at "frameworks/base/telephony/java/android/telephony/SignalStrength.java" Believe me, I just got out of weeks of work porting a new GSM modem for my embedded platform and I had to look through everything to get it to work and I even found a bug where Android doesn't handle properly the weak signal when the modem falls into "limited connectivity", "emergency calls only", it turns into a lost data connection deadlock. I fixed it. – Sylvain Huard Oct 02 '12 at 13:39
  • Oh wow! thats great. I do not have the android source code will look forward on how to get it. Till then i am wondering how can i access method of class SignalStrength without actually initializing its object. I do not find a way to initialize its object and without its object how can i access its method. – Calvin Oct 03 '12 at 06:07
  • The listener is just a very efficient way to inform your app that something happened related to what you are registered to listen. It's a lot more efficient than constantly polling something. For what you want, you have to "import android.telephony.SignalStrength". Look at the example in the answer I provided, I will edit it in the next few minutes and you will see. – Sylvain Huard Oct 03 '12 at 14:33
  • Thank for the code, i had already implemented the listener to get the signal strength in my app, my requirement is to get the signal strength only once in the app. what i am doing right now is unregistering the listener after getting the strength. what i was looking is to get the strength without implementing listener. – Calvin Oct 05 '12 at 04:56
  • That's what I'm saying, you don't need to implement the listener at all. Don't register it and it will work because the GetGsmSignalStrength is just code that will read the data structure holding the information. And that information is updated by the RIL the listener is just a message you can register too to get notified that there is a change but is not necessary to just get the info. – Sylvain Huard Oct 05 '12 at 12:27
  • I don't think you can have a SignalStrength object without a listener. And in the last version of the source code, i can also say that each Listener will get a *different* SignalStrength object. – pcans Feb 21 '13 at 12:47