Here is how to turn on and turn off wifi in android.
First you need to declare the following in your manifest file:
Now in your activity, do this to control WiFi:
Complete reference to the WiFi code in androidFirst you need to declare the following in your manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
Now in your activity, do this to control WiFi:
private WifiManager wifiManager;
@Override
public void onCreate(Bundle icicle) {
-- your usual stuff --
// Get the Wifi service from our system
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
// Check the our wifi is currently turned on or turned off
if(wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(false);
}else{
wifiManager.setWifiEnabled(true);
}
}
Source:
No comments:
Post a Comment