Friday, March 23, 2012

How will you programatically enable or disable WIFI in android?


How will you programatically enable or disable WIFI in android?

Hello all..
This is a simple code snippet to enable WI-FI in android programatically in android.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
public boolean enableWIFI()
{
        WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        if(wifiManager.isWifiEnabled()){
             if(wifiManager.setWifiEnabled(false))
                return true;
          }else{
            if(wifiManager.setWifiEnabled(true))
                 return true;
        }
        return false;
}
Note : Make sure to add the permissions in the manifest file.
These are the permissions.
?
Drag and copy the code
1
2
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Please leave your valuable comments on this post.

No comments: