Note that there are some explanatory texts on larger screens.

plurals
  1. PORead WIFI_SLEEP_POLICY on Nexus 7
    primarykey
    data
    text
    <p>I'm building an app that must keep the Wi-Fi connection alive in the background. Therefore it is important that the device doesn't auto shutdown Wi-Fi when the device goes to sleep.</p> <p>Therefore I would like to check for the current Wi-Fi Sleep policy on the device, and if it is the wrong one, I would like to inform the users about it and link them to the right settings page so they can change it.</p> <p>In order to do this I`ve written the following code:</p> <pre><code>@SuppressWarnings("deprecation") @SuppressLint({ "InlinedApi", "NewApi" }) private void checkWiFiPolicy() { ContentResolver cr = this.getContentResolver(); int apiLevel = 0; try { apiLevel = Build.VERSION.SDK_INT; } catch (Exception e) {} int apiIndWifiSleepPolicyDefault = -1; int apiIndWifiSleepPolicyNever = -1; int apiIndWifiSleepPolicyNeverWhilePlugged = -1; int currentWifiPolicy = -10; if (apiLevel &lt; 17) { apiIndWifiSleepPolicyDefault = Settings.System.WIFI_SLEEP_POLICY_DEFAULT; apiIndWifiSleepPolicyNever = Settings.System.WIFI_SLEEP_POLICY_NEVER; apiIndWifiSleepPolicyNeverWhilePlugged = Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED; try { currentWifiPolicy = Settings.System.getInt(cr, android.provider.Settings.System.WIFI_SLEEP_POLICY); } catch (Exception e) {} } else { apiIndWifiSleepPolicyDefault = Settings.Global.WIFI_SLEEP_POLICY_DEFAULT; apiIndWifiSleepPolicyNever = Settings.Global.WIFI_SLEEP_POLICY_NEVER; apiIndWifiSleepPolicyNeverWhilePlugged = Settings.Global.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED; try { currentWifiPolicy = Settings.Global.getInt(cr, android.provider.Settings.Global.WIFI_SLEEP_POLICY); } catch (Exception e) { e.printStackTrace(); } } if (currentWifiPolicy == apiIndWifiSleepPolicyDefault) { ALog.d(ALog.TAG_ACTIVITY, "Detected WifiSleepPolicy: DEFAULT"); } else if (currentWifiPolicy == apiIndWifiSleepPolicyNever) { ALog.d(ALog.TAG_ACTIVITY, "Detected WifiSleepPolicy: NEVER"); } else if (currentWifiPolicy == apiIndWifiSleepPolicyNeverWhilePlugged) { ALog.d(ALog.TAG_ACTIVITY, "Detected WifiSleepPolicy: NEVER WHILE PLUGGED"); } else { ALog.d(ALog.TAG_ACTIVITY, "Detected WifiSleepPolicy: UNKNOWN"); } } </code></pre> <p>This code however fails on a Nexus 7 (Android 4.3, Gen. 1):</p> <pre><code>W/System.err(5318): android.provider.Settings$SettingNotFoundException: wifi_sleep_policy </code></pre> <p>Even if I try the legacy approach (the one I use for apiLevel &lt; 17), I get exactly the same error. (Note that I am able to read other System settings such as System.Global.DATA_ROAMING)</p> <p>Any Ideas on how I can read this Setting on a Nexus 7?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload