public
Created

Android: switch wifi on/off

  • Download Gist
AndroidManifest.xml
XML
1 2 3
<!-- 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"/>
activity.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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.
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.