title: Aquire a "network" lock that locks wifi when it's used, but doesn't when 3G is used?
body: <p>Currently I have this in my code:</p>
  
  <pre><code>WifiManager.WifiLock wifiLock = ((WifiManager) context
      .getSystemService(Context.WIFI_SERVICE))
      .createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock");
  wifiLock.aquire();
  </code></pre>
  
  <p>However my app uses "any" type of network. When there's no Wifi connection it automatically uses 3G data. Is there a way to optimize this code somehow so that it only locks WiFi if it's actually being used? The only way I can think of doing it is to change the above code to something like:</p>
  
  <pre><code>PreferenceManager
      .getDefaultSharedPreferences(getContext())
      .edit()
      .putBoolean(PREF_WIFI_LOCK_NEEDED, true)
      .apply();
  </code></pre>
  
  <p>and creating some NetworkStateReceiver that listens to WifiManager.NETWORK_STATE_CHANGED_ACTION and/or ConnectivityManager.CONNECTIVITY_ACTION broadcasts and aquire/release the lock somehow (and probably saving the lock to a static member of my Application?)</p>
  
  <p>Is there a recommended way to do this?</p>