Vinoth
Vinoth

Reputation: 1359

Toggling state of Wifi in Android device

I have to make an app in which I need to display the status of the WiFi on a RadioGroup (Either its "on" or "off"). I want the default behavior for the application to show the status of the Wifi when the user clicks the SettingsPage. The RadioGroup must check the status of the Wifi and display it accordingly("on" or "off"). I must also be able to toggle the states of the Wifi.

I hope the images below can make my question clear.

Thanks for your time.

enter image description here

Upvotes: 1

Views: 939

Answers (2)

Valeh Ağayev
Valeh Ağayev

Reputation: 596

To Enable WiFi:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);

Note: To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file:

android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS 
android.permission.CHANGE_WIFI_STATE

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Check this question: Android: How to Enable/Disable Wifi or Internet Connection Programmatically

And make sure the radio button is selected if the wifi is on otherwise make radio button as not selected.

More info about checking wifi status:

WifiManager wifi = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
boolean b=wifi.isWifiEnabled();

Upvotes: 4

Related Questions