Reputation: 634
Why we have to set up a listener in order to get signal strength of single current cell when in 3g mode?
We get signal strengths of neighbouringcell using NeghbouringcellInfo class?
Why cannot we get signal strength of current cell like that?
Upvotes: 1
Views: 16154
Reputation: 3412
try the following code:
ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
if (is3g) {
myListener = new MyPhoneStateListener();
TelephonyManager telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(myListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
private class MyPhoneStateListener extends PhoneStateListener {
public int singalStenths = 0;
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
int singalStrength = signalStrength.getGsmSignalStrength();
singalStenths = signalStrength.getGsmSignalStrength();
System.out.println("----- gsm strength" + singalStrength );
System.out.println("----- gsm strength" + singalStenths );
if (singalStenths > 30) {
signalstrength.setText("Signal Str : Good");
signalstrength.setTextColor(getResources().getColor(R.color.good));
}
else if(singalStenths > 20 && singalStenths < 30) {
signalstrength.setText("Signal Str : Average");
signalstrength.setTextColor(getResources().getColor(R.color.average));
}
else if(singalStenths < 20) {
signalstrength.setText("Signal Str : Weak");
signalstrength.setTextColor(getResources().getColor(R.color.weak));
}
}
};
Upvotes: 7
Reputation: 1398
If I understand well, you don't want to set up a listenner but just get the signal strength whenever you need it. If it's the case here is how to do it:
android.telephony.SignalStrength
You would get the 3G or CDMA or whatever type of cell you are connected to.
hope it helps.
-------- EDIT after comments --------------
The listener is just a nice way to be informed that there is a change in what you are interested in without having to constantly poll it. It's much more efficient.
Here is an example I took from Android source code.
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
String updateMsg = "cdma dBM=" + signalStrength.getCdmaDbm();
mTrackerData.writeEntry(SIGNAL_PROVIDER_TAG, updateMsg);
} else if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
String updateMsg = "gsm signal=" + signalStrength.getGsmSignalStrength();
mTrackerData.writeEntry(SIGNAL_PROVIDER_TAG, updateMsg);
}
}
This is taken from frameworks/base/tests/LocationTracker/src/com/android/locationtracker/TrackerService.java
.
Yes this method is called by a listener but it doesn't have to. it's just a much more efficient way to do things than constantly polling. On the other hand, if you just want the signal strength at a specific time, just one shot, nothing prevents you from just calling it from somewhere else.
Also, I suggest you download your Android distribution. With "grep", it gives a lot of examples on how classes, methods etc are used. I'm learning a lot from it. These examples are good since they have been coded by the people who wrote the OS. Also, you can get the source code at Android source code
Upvotes: 0
Reputation: 2191
I tried to use the "Hazmat" code, but unfortunately does not work on my device (Samsung S3 mini Android 4.1.2). It seems that some phones from Samsung have problems with this feature and nobody from Samsung cares. Also other Samsung models have similar problem:
Samsung Galaxy SI -> no problems
Samsung Galaxy SII -> no strength and no refresh
Samsung Galaxy Tab 10.1 -> no strength and no refresh
Samsung Galaxy Note -> no strength and no refresh
(Android Versions: 2.3.3, 2.3.4, 2.3.5, 3.1)
Conclusion: Be aware that a lot of Samsung devices don´t work properly with this function! I am not aware of any workaround for it. Samsung makes our apps unreliable.
Upvotes: 3
Reputation: 634
the implementation is such that we have to implement listener to get signal strength of current cell, i have wrote the listener and stopped the listener at first read and then kept in variable. no any other way
Upvotes: 3