Amazon Gift Cards

Sunday, October 18, 2009

Turn off, Turn on wifi in android using code tutorial

Here is how to turn on and turn off wifi in android.

First 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>

After doing it that on your Activity class
private WifiManager wifiManager;
@Override 
public void onCreate(Bundle icicle) {
  ....................
  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  if(wifiManager.isWifiEnabled()){
    wifiManager.setWifiEnabled(false);
  }else{
    wifiManager.setWifiEnabled(true);
  }
}


Explanation
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()){

Turn on/off our wifi
wifiManager.setWifiEnabled(<true|false>);

Reference
WifiEnabler
Permissions Journey: ACCESS_WIFI_STATE

Update History
   Jan 17, 2012 - Visual Update

6 comments:

Almond Mendoza » Turn off, Turn on wifi in android using code tutorial Android Marketing said...

[...] here to read the rest:  Almond Mendoza » Turn off, Turn on wifi in android using code tutorial By admin | category: android, android tutorial | tags: behold, japan, knowledge, [...]

Anonymous said...

Thanks :)

Shubham said...

thanks,do u have any idea how to get ip for all devices connected in wifi network.

thanks

Michael said...

Thanks! Do you know how to do something similar with the phone's data connection?

Anonymous said...

excelent guide

fahnoa said...

Is there a way to have a tech just program a new wifi cause for some reason my phone wont turn on wifi since it was flashed