Android: switch wifi on/off
public void toggleWifi() { | |
// Get the WifiManager service from the context | |
WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); | |
// Read the current state. True is On, False is Off | |
boolean currentState = wm.isWifiEnabled(); | |
// And now, switch to the other state | |
wm.setWifiEnabled(!currentState); | |
// You're done :) | |
// Note that it can take some time for the wifi to reconnect | |
// when you switch it on. | |
} |
<!-- Add these permissions to your android manifest --> | |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> | |
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment