问题
I want to get the Signal Strength of the Device at the point I hit the API call. I have searched on all the related threads and I am not successful yet.
So I would like to get the signal strength like
SignalStrength ss = null ; // some initialization
int n = ss.getGsmSignalStrength();
But while using this, it is obvious that I will get null pointer exception since I have initialised SignalStrength
as null. But I don\'t know how to initialise this.
Also that I don\'t want to use PhoneStateListener
because it is triggered only if the signal changes.
I am getting the Signal Strength using the below code
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
But I don\'t want to use CellSignalStrength
because it is only added in API Level 17 and will not work under 17. I want the code to work on API Level 7+.
Or is there any other method, so that I could get the signal strength at the point of hitting the API call?
回答1:
Define Variables:
TelephonyManager mTelephonyManager;
MyPhoneStateListener mPhoneStatelistener;
int mSignalStrength = 0;
Then add this class to your code:
class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
mSignalStrength = signalStrength.getGsmSignalStrength();
mSignalStrength = (2 * mSignalStrength) - 113; // -> dBm
}
}
and in your onCreate
method use:
mPhoneStatelistener = new MyPhoneStateListener();
mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneStatelistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
回答2:
Global Define :
TelephonyManager telephonyManager;
myPhoneStateListener psListener;
TextView txtSignalStr;
onCreate
Method :
@Override
protected void onCreate(final Bundle savedInstanceState) {
txtSignalStr = (TextView)findViewById(R.id.signalStrength);
psListener = new myPhoneStateListener();
telephonyManager = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(psListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
Create myPhoneStateListener
Class :
public class myPhoneStateListener extends PhoneStateListener {
public int signalStrengthValue;
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
if (signalStrength.isGsm()) {
if (signalStrength.getGsmSignalStrength() != 99)
signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
else
signalStrengthValue = signalStrength.getGsmSignalStrength();
} else {
signalStrengthValue = signalStrength.getCdmaDbm();
}
txtSignalStr.setText("Signal Strength : " + signalStrengthValue);
}
}
回答3:
public class PhoneCustomStateListener extends PhoneStateListener {
public int signalSupport = 0;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
signalSupport = signalStrength.getGsmSignalStrength();
Log.d(getClass().getCanonicalName(), "------ gsm signal --> " + signalSupport);
if (signalSupport > 30) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Good");
} else if (signalSupport > 20 && signalSupport < 30) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Avarage");
} else if (signalSupport < 20 && signalSupport > 3) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Weak");
} else if (signalSupport < 3) {
Log.d(getClass().getCanonicalName(), "Signal GSM : Very weak");
}
}
}
回答4:
We should not initialize signalstrength, instead of that use phonelistener and override the method onSignalStrengthsChanged(SignalStrength signalStrength).
For eg., have a look at following code snippet
class SamplePhoneStateListener extends PhoneStateListener {
int signalStrength = 0;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
signalStrength = signalStrength.getGsmSignalStrength();
//You can check the signal strength value here..
}
}
using TelephonyManager object you can listen to the above class like
TelephonyManagerObject.listen(myListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
回答5:
This worked for me:
Which services are needed:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
How to gather the metrics:
public void startGatherMetrics() {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
networkInfoStr = connectivityManager.getActiveNetworkInfo().toString();
// gather Network Capabilities
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
networkInfoStr += "; " + connectivityManager.getNetworkCapabilities(network).toString();
}
}
Log.d("A_NETWORK_INFO", networkInfoStr);
new Thread(new Runnable() {
@Override
public void run() {
quitLooper = false;
Looper.prepare();
telephonyManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
Log.d("A_NETWORK_METRICS",
"Signal Strength (0-4 / dBm):" + getLevel(signalStrength) + " / "
+ getDbm(signalStrength));
if (quitLooper)
Looper.myLooper().quit();
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
Looper.loop();
}
}).start();
}
public void stop() {
quitLooper = true;
}
The "getLevel(signalStrength)" and "getDbm(signalStrength)" can be found here.
回答6:
You don't instantiate SignalStrength (and possibly you cannot). from application code.
You must use a PhoneStateListener (subclass), and implement onSignalStrengthsChanged:
http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onSignalStrengthsChanged(android.telephony.SignalStrength)
A SignalStrength will be created for you and passed into your override.
回答7:
The PhoneStateListener object should be instantiated in a looper thread. So, if you are using worker thread (non - GUI thread) the following can be used;
private HandlerThread handlerThreadCellularSignal = null;
public void startListen() {
handlerThreadCellularSignal = new HandlerThread("CELLULAR_INFO_THREAD");
handlerThreadCellularSignal.start();
Looper looper = handlerThreadCellularSignal.getLooper();
Handler handler = new Handler(looper);
handler.post(new Runnable() {
@Override
public void run() {
phoneStatelistener = new PhoneStateListenerEx();
TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStatelistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
});
}
private void stopListen() {
handlerThreadCellularSignal.quit();
}
public class PhoneStateListenerEx extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
int signalStrengthdBm = (2 * signalStrength.getGsmSignalStrength()) - 113; // -> dBm
Log.d("Cellular Signal Strength | " + String.valueOf(signalStrengthdBm));
}
}
来源:https://stackoverflow.com/questions/19805880/get-signal-strength-in-android