Is it possible to toggle WiFi radio (On/Off) programmatically on an开发者_JS百科droid phones?
Have a look at the WifiManager: http://developer.android.com/reference/android/net/wifi/WifiManager.html
Specifically:
boolean setWifiEnabled(boolean enabled)
Enable or disable Wi-Fi.
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
To actually toggle, meaning, switching the state, use:
WifiManager wm = ((WifiManager) activity.getSystemService(Context.WIFI_SERVICE));
wm.setWifiEnabled(!wm.isWifiEnabled());
and add the permissions:
android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS
android.permission.CHANGE_WIFI_STATE
精彩评论