7

I have used the following code to get the signal strength,

    SignalStrengthListener signalStrengthListener;
    signalStrengthListener = new SignalStrengthListener();
    ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(
            signalStrengthListener,
            SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);

and then it is listening for the Signal strength,

private class SignalStrengthListener extends PhoneStateListener {
    @Override
    public void onSignalStrengthsChanged(
            android.telephony.SignalStrength signalStrength) {

        // get the signal strength (a value between 0 and 31)
        int strengthAmplitude = signalStrength.getGsmSignalStrength();

        // do something with it (in this case we update a text view)
        // signalStrengthText.setText(String.valueOf(strengthAmplitude));
        if (strengthAmplitude > 30) {
            signalStrengthText.setText("Good");
            // signalStrengthText.setTextColor(getResources().getColor(R.color.good));
        } else if (strengthAmplitude > 20 && strengthAmplitude < 30) {
            signalStrengthText.setText("Average");
            // signalStrengthText.setTextColor(getResources().getColor(R.color.average));
        } else if (strengthAmplitude < 20) {
            signalStrengthText.setText("Weak");
            // signalStrengthText.setTextColor(getResources().getColor(R.color.weak));
        }

        super.onSignalStrengthsChanged(signalStrength);
    }
}

It works good if the sim is present in the device. But when I remove the sim from the device and then check for the signal strength, it still provides some value for the signal strength.

One possible solution, I can think of is to first check, if the sim is present in the device or not and then show the signal strength. But I would like to know an explanation for this weird behaviour and a possible solution for it.

Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • I had the same problem but after a restart of phone - I didn't get any signal strength. So probably the signal strength doesn't get reset automatically until the switch off. – TheDevMan Oct 17 '13 at 09:18

2 Answers2

4

no USIM is required for cell service - only for authentication. else emergency calls would fail.

it's not weird at all... that is common sense, since you do not remove the radio nor disable it.

a simple test: remove the USIM, call emergency services, pretend you were pocket dialing.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

Before you're checking the Signal Strength, you could possibly check is the device having SIM card or not (if you're concerned about WIFI network then you need to handle that separately) then check for Signal Strength. you could try something like

public boolean isSimAvailable() {
boolean isAvailable = false;
TelephonyManager telMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT: //SimState = “No Sim Found!”;
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED: //SimState = “Network Locked!”;
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED: //SimState = “PIN Required to access SIM!”;
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED: //SimState = “PUK Required to access SIM!”; // Personal Unblocking Code
break;
case TelephonyManager.SIM_STATE_READY:
isAvailable = true;
break;
case TelephonyManager.SIM_STATE_UNKNOWN: //SimState = “Unknown SIM State!”;
break;
}
return isAvailable;
}

///When you get the signal strength, Check like

SignalStrengthListener signalStrengthListener;
    if(isSimAvailable()){
         signalStrengthListener = new SignalStrengthListener();
         ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).listen(
            signalStrengthListener,
            SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
    } else {
         //alert the user or do other stuff.
    }
BharathRao
  • 1,846
  • 1
  • 18
  • 28