Sie sind auf Seite 1von 5

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

Get Signal Strength in Android


5

android

telephony

signal-strength

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 ?

share

improve this question


VIGNESH
802 4

15 38

Asked
Nov 6 '13 at 6:55

Possible duplicate of How to get cell service signal strength in Android? blahdiblah Nov 6 '13 at 6:57
@blahdiblah : but i dont want to use phonestatelistener . I have referred that link and that would not
help me . VIGNESH Nov 6 '13 at 9:52

You said you didn't want to use PhoneStateListener because it's only triggered on changes, but as that
other question indicates, it'll fire when your app starts up. At that point, you should only care when it
changes. blahdiblah Nov 6 '13 at 17:46

add a comment

order by votes

4 Answers

Define Variables:
TelephonyManager TelephonManager;
myPhoneStateListener pslistener;
int SignalStrength = 0;

Then add this class to your code:


class myPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
SignalStrength = signalStrength.getGsmSignalStrength();
SignalStrength = (2 * SignalStrength) - 113; // -> dBm
}
}

and in your onCreate method use:


try {
pslistener = new myPhoneStateListener();
TelephonManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVI
CE);
TelephonManager.listen(pslistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
catch (Exception ex) {
ex.printStackTrace();
}

share

improve this answer


Ingo
654

Bahman_Aries
3,284 4 14

Answered
Sep 26 '14 at 10:50

7 12

33

Edited
Aug 12 '15 at 6:23

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.

share

improve this answer


Answered
Apr 26 '14 at 14:41

user2579822

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)"

share

improve this answer


Sid
461

4 6

Answered
Jul 22 '14 at 21:41

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;

share

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);
}

improve this answer


reegan29
142 1

1 16

Answered
Jun 19 '15 at 12:01

Your Answer

log in

or
Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Das könnte Ihnen auch gefallen