Code example for WifiManager.WifiLock

Methods: acquire, isHeld, release, setReferenceCounted

3
  public static void keepWiFiOn(Context context, boolean on) {
    if (wifiLock == null) {
      WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      if (wm != null) {
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
        wifiLock.setReferenceCounted(true);
      } 
    } 
    if (wifiLock != null) { // May be null if wm is null
      if (on) {
        wifiLock.acquire();
        L.d(TAG, "Adquired WiFi lock");
      } else if (wifiLock.isHeld()) {
        wifiLock.release();
        L.d(TAG, "Released WiFi lock");
      } 
    } 
  } 
} 
Stop searching for code, let great code find you!  Add Codota to your java IDE