How to get cell service signal strength in Android?
Asked Answered
F

3

24

I am trying to write a very simple Android application that checks the signal strength of the current cell. So far, I have only found something called getNeighboringCellInfo(), but I'm not really sure if that includes the current cell.

How do I get the CURRENT cell signal strength in Android?

Does getNeighborCellInfo() get the current cell? It doesn't seem like it based on the results that I have been able to get with it. Here's my current code:

List<NeighboringCellInfo> n = tm.getNeighboringCellInfo();

//Construct the string
String s = "";
int rss = 0;
int cid = 0;
for (NeighboringCellInfo nci : n)
{
    cid = nci.getCid();
    rss = -113 + 2*nci.getRssi();
    s += "Cell ID: " + Integer.toString(cid) + "     Signal Power (dBm): " + 
            Integer.toString(rss) + "\n";
}

mainText.setText(s);
Flambeau answered 27/12, 2009 at 21:48 Comment(0)
T
21

create a PhoneStateListener and handle the onSignalStrengthChanged callback. When your app is initialized, it should give you an initial notification. This is in 1.x. in 2.x, there's an open issue about this.

Thacker answered 27/12, 2009 at 22:1 Comment(1)
Any solution for 4.x - 8.x ?Markhor
F
14

This code may help:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
public void onCallForwardingIndicatorChanged(boolean cfi) {}
public void onCallStateChanged(int state, String incomingNumber) {}
public void onCellLocationChanged(CellLocation location) {}
public void onDataActivity(int direction) {}
public void onDataConnectionStateChanged(int state) {}
public void onMessageWaitingIndicatorChanged(boolean mwi) {}
public void onServiceStateChanged(ServiceState serviceState) {}
public void onSignalStrengthChanged(int asu) {}
};

Once you’ve created your own Phone State Listener, register it with the Telephony Manager using a bitmask to indicate the events you want to listen for, as shown in the following code snippet:

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
PhoneStateListener.LISTEN_CALL_STATE |
PhoneStateListener.LISTEN_CELL_LOCATION |
PhoneStateListener.LISTEN_DATA_ACTIVITY |
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
PhoneStateListener.LISTEN_SERVICE_STATE |
PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

Also, need to add these to AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Finnegan answered 16/10, 2011 at 0:36 Comment(3)
void onSignalStrengthChanged(int asu) is deprecated, use void onSignalStrengthsChanged( SignalStrength signalStrength ) insteadKelly
@SamSch if you use onSignalStrengthsChanged you should also use PhoneStateListener.LISTEN_SIGNAL_STRENGTHS in the mask, instead of PhoneStateListener.LISTEN_SIGNAL_STRENGTHVerbify
@Simone, yes, you're right. sorry that i missed that detailKelly
T
0

enter image description hereBelow code will create effect like picture (Android system Cell Info)

Inside your running activity/fragment, create a sub class like this

TextView txtSignalMobile1 = findViewById...;
    class myPhoneStateListener extends PhoneStateListener {
        public int signalStrengthValue;
        public int signalLevel;

        @RequiresApi(api = Build.VERSION_CODES.Q)
        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
            super.onSignalStrengthsChanged(signalStrength);
            if (signalStrength.isGsm()) {
                if (signalStrength.getGsmSignalStrength() != 99)
                    signalStrengthValue = signalStrength.getGsmSignalStrength() * 2 - 113;
                else{
                    signalStrengthValue = signalStrength.getCellSignalStrengths().get(0).getDbm();
                    signalLevel =signalStrength.getCellSignalStrengths().get(0).getAsuLevel();
                }


            } else {
                signalStrengthValue = signalStrength.getCdmaDbm();
            }
            txtSignalMobile1.setText(signalStrengthValue + "dbm, " + signalLevel + "asu");
        }
    }

Call somewhere else maybe onCreate or after a button... (used thread to update continnously value changed)

Thread splashThread2 = new Thread() {
            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(1000);

                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // update TextView here!
                                int signal2 = NetworkUtils.GetWifiSignal(requireContext());
                                txtSignalWifi.setText(signal2 + "/100");
                            }
                        });
                    }
                } catch (Exception ignore) {
                    // when user exit suddenly
                }
            }
        };
        splashThread2.start();
Tombac answered 20/12, 2021 at 2:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.