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 ?
Define Variables:
Then add this class to your code:
and in your
onCreate
method use:This worked for me:
Which services are needed:
How to gather the metrics:
The "getLevel(signalStrength)" and "getDbm(signalStrength)" can be found here.
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.
Global Define :
onCreate
Method :Create
myPhoneStateListener
Class :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
using TelephonyManager object you can listen to the above class like