Get Signal Strength in Android

2019-01-11 04:24发布

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 ?

6条回答
我只想做你的唯一
2楼-- · 2019-01-11 04:43

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);
查看更多
三岁会撩人
3楼-- · 2019-01-11 04:48
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楼-- · 2019-01-11 04:50

This worked for me:

Which services are needed:

@SystemService
protected ConnectivityManager connectivityManager;

@SystemService
protected TelephonyManager telephonyManager;

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.

查看更多
相关推荐>>
5楼-- · 2019-01-11 04:54

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.

查看更多
戒情不戒烟
6楼-- · 2019-01-11 04:55

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);
    }
}
查看更多
Luminary・发光体
7楼-- · 2019-01-11 05:00

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)
查看更多
登录 后发表回答