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
How to Check Which Notifications Are Active in Status Bar in Android Dev
Change the Text Color of Numberpicker
Android: Programmatically Detect If Device Has Hardware Menu Button
Android: Implementing Progressbar and "Loading..." for Endless List Like Android Market
Creating Custom Lockscreen in Android
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
How to Save Image in Android Gallery
Android Java.Lang.Noclassdeffounderror
How to Create a Custom Notification Layout in Android
How to Style the Drawerarrowtoggle from Android Appcompat V7 21 Library
Getting Access to Media Player Cache
Android Runtimeexception: Unable to Instantiate the Service
How to Detect When a User Plugs Headset on Android Device? (Opposite of Action_Audio_Becoming_Noisy)
Android: Cursoradapter, Listview and Checkbox