开发者

Toggle WIFI radio programmatically

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜