How to Get Cell Service Signal Strength in Android

How to get cell service signal strength in Android?

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.

How to get current cell signal strength?

There is getAllCellInfo() method in TelephonyManager added in API 17 that could be good solution. Example of use:

TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
// for example value of first element
CellInfoGsm cellInfoGsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();

Get current/statechanged cell signal strength. onStateSignalChanged never invoked

First Define below mentioned variables globally :

TelephonyManager telephonyManager;
MyPhoneStateListener psListener;
TextView txtSignalStr;

onCreate Method :

@Override
protected void onCreate(final Bundle savedInstanceState) {
psListener = new MyPhoneStateListener();
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(psListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

Create an inner class that will extend PhoneStateListener

and use @Override annotation before onSignalStrengthsChanged method like

Create MyPhoneStateListener Class :

public class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
//your Code
}
}

Get signal strength of WIFI and Mobile Data

Wifi:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();

In case of mobile it should work:

TelephonyManager telephonyManager =        (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();

Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi



Related Topics



Leave a reply



Submit