Tuesday, September 7, 2010

Turn off / on wifi in android using code


AIM:  To turn on and turn off wifi in android. 


SOLUTION : 
  •  Declare the following in your manifest file. 
< uses-permission android:name="android.permission.ACCESS_WIFI_STATE" / > < uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/ > < uses-permission android:name="android.permission.CHANGE_WIFI_STATE" / > < uses-permission android:name="android.permission.WAKE_LOCK" / >



  • In  your Activity class
private WifiManager wifiManager;
@Override
public void onCreate(Bundle icicle) { 
       
       // 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); // Turn on/off our wifi
  }else{
    wifiManager.setWifiEnabled(true);
  }
}




No comments:

Post a Comment

Android Developers Blog

Ram's shared items

Amazon Product Ads