0

I am trying to show the current signal strength and current cell ID and Lac in my application for 3g network. Since it has to be compatible for API-8, I am using SignalStrength class from android.telephony. When I click a button, for now I want it to show the CID, Lac and signal strength of current cell. I am getting the CID and lac but the signal strength is always showing 0. The code is given below:

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bShowCell:
        GsmCellLocation location;
        String cellID = "";
        String lac = "";
        Context context = (Context) getApplicationContext();
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        location = (GsmCellLocation) tm.getCellLocation();
        cellID = String.valueOf(location.getCid());
        lac = String.valueOf(location.getLac());
        CurCell.setText(cellID);
        CellLac.setText(lac);
        CurStrgth.setText(getRSSI());
    }
}

public String getRSSI() {
    MyListener = new MyPhoneStateListener();
    Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    String strength = MyListener.getStrength();
    return strength;
}

class MyPhoneStateListener extends PhoneStateListener {

    public int singalStrengths;

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        int asu = signalStrength.getGsmSignalStrength();
        singalStrengths = -113 + 2 * asu;
    }

    public String getStrength() {
        return String.valueOf(singalStrengths);
    }
}

I have checked many examples online and I think my code is okay. But when I checked it in debug mode, I see that when I click the button, the program never goes in onSignalStrengthsChanged. Is there anything I am missing?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Lutful Kabir
  • 3
  • 1
  • 6
  • do you register your listener ? do you have the permissions ? if yes try making `singalStrengths` static - or even better store it in preferences. Even so it won't show the latest signal - see http://stackoverflow.com/questions/3888775/instant-signal-strength – Mr_and_Mrs_D Jul 08 '13 at 13:21
  • well It was long ago, I remember like when I cancelled the getRSSI() method and put the listener and .getStrength() inside the OnCreate() directly, it worked fine. But to be honest Still don't know why it worked fine. – Lutful Kabir Jul 08 '13 at 13:52
  • Post it as an answer - I don't know why it worked - are you sure it was the only change you made ? – Mr_and_Mrs_D Jul 08 '13 at 13:58
  • as you asked I posted the Code I found in that app I did, I did some changes to experiment, but only this way I get the updates regularly – Lutful Kabir Jul 08 '13 at 15:31

1 Answers1

0

Well, this is how I tried later and it worked fine. I had to make the CurStrgth.setText()independent from the Button and inside the PhoneStateListener and called the listener from OnCreate() method. And it works fine, updates the CurStrgth TextView whenever it gets a change in the signal strength. My code is given below:

public class MainActivity extends Activity implements OnClickListener {


TextView CurStrgth;
MyPhoneStateListener MyListener;
TelephonyManager Tel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CurStrgth = (TextView) findViewById(R.id.tvCurCellStrength);

    MyListener = new MyPhoneStateListener();

    Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);


}


@Override
protected void onPause() {
    super.onPause();
    Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE);
}

@Override
protected void onResume() {
    super.onResume();
    Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

private class MyPhoneStateListener extends PhoneStateListener {
    String gsmStrength = "";

    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
            gsmStrength = String
                .valueOf(signalStrength.getGsmSignalStrength() * 2 - 113);
        CurStrgth.setText(MyListener.getStrength() + "dBm");
    }

    public String getStrength() {
        return gsmStrength;
    }

}
}

Now, I checked just calling the

    CurStrgth.setText(MyListener.getStrength() + "dBm");

inside the OnClick() method for a button and it shows only the value once but If I press the button later It never updates the Strength and keeps showing the initial value.

Lutful Kabir
  • 3
  • 1
  • 6
  • Probably in your initial implementation it had no time to update the variable - see [here](http://stackoverflow.com/a/5832912/281545). "It never updates the Strength and keeps showing the initial value" --> even when the value does really change ? (see http://stackoverflow.com/a/3720676/281545 : the listener is only called in big signal changes). EDIT : Please! do not use variable names starting with capital letter (your formatting is better - but avoid unneeded empty lines). EDIT2 : why you register both onResume and onCreate ? EDIT3 : accept your answer :) – Mr_and_Mrs_D Jul 08 '13 at 17:39
  • Actually yah, it changed auto when there is a big change and yet I dont think its possible that you can control the listening of the Listener. But I was planning that when I already initiated the Listener and registered it, if I could just with a button click get the latest value from that Listeners updated value, it would work, but it seems that just works for the first time I click the button, afterwards it keeps showing the same value even If I changed my room! and thanks for the edits :D for registering actually yah it was like one of my very early codes.. – Lutful Kabir Jul 08 '13 at 23:55
  • welcome - maybe check http://stackoverflow.com/questions/3888775/instant-signal-strength/17533439#17533439 - maybe gives you more up to date signal - add @Mr_and_Mrs_D (example) if you want users to be notified of your comments - I just saw this accidentally – Mr_and_Mrs_D Jul 09 '13 at 00:04