问题
I wrote a little Android program to detect cellular signal strength. I inserted an expired SIM card into my phone, it displayed zero signal in status bar. However, the following code returns Signal strength equals to 22.
public class WelcomeActivity extends AppCompatActivity {
TelephonyManager telephonyManager;
CustomPhoneStateListener customPhoneStateListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
customPhoneStateListener = new CustomPhoneStateListener();
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
@Override
protected void onPause() {
super.onPause();
telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
@Override
protected void onResume() {
super.onResume();
telephonyManager.listen(customPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
/* Phone State Listener */
private class CustomPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
int strength = signalStrength.getGsmSignalStrength();
Toast.makeText(getApplicationContext(), "Signal Strength: " + String.valueOf(strength), Toast.LENGTH_SHORT).show();
}
}
}
Where does the value 22 come from? How can I detect whether the cellular network is connected or not? Is this value really reliable?
From Android documentation, the getGsmSignalStrength()
returns value of 0 to 31 and 99.
回答1:
I have just implemented n Run Code:
package com.example.sanketprabhu.gsmsignalstrength;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
TelephonyManager Tel;
MyPhoneStateListener MyListener;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
public class MyPhoneStateListener extends PhoneStateListener
{
/* Get the Signal strength from the provider, each tiome there is an update */
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = "
+ String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show();
}
}
}
It works for me.
Here I have attached Some Screen shots.
Yo are right:Values in betn 0-31 & 99
Unfortunately I am in office where signal strength is low, but works
来源:https://stackoverflow.com/questions/33931989/android-still-detects-cellular-signal-strength-while-no-cellular-network-connect