Search code examples
javaandroidlocationsignal-strength3g-network

My PhoneStateListener is not being called for showing signal strength


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?


Solution

  • 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.