Get Signal Strength in Android

时光怂恿深爱的人放手 提交于 2019-11-27 11:56:49
Ingo

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

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


        }
    }
}
Sid

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)

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.

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.

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));
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!