Skip to content

Instantly share code, notes, and snippets.

@aspyct
Created May 4, 2013 10:02
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aspyct/5517026 to your computer and use it in GitHub Desktop.
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