开发者

Toggle WIFI radio programmatically

开发者 https://www.devze.com 2023-01-03 06:57 出处:网络
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

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

精彩评论

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