Created
May 4, 2013 10:02
-
-
Save aspyct/5517026 to your computer and use it in GitHub Desktop.
Android: switch wifi on/off
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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