WIFI_SLEEP_POLICY_NEVER, how to set in API-17?

I used the following code, my goal is API-15

android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_SLEEP_POLICY, android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER); 

When the code runs on the API-17 platform, I get warnings in logcat,

The wifi_sleep_policy setting has moved from android.provider.Settings.System to android.provider.Settings.Global, the value has not changed.

So, what I did after this, my APi-17 project goal was set and used this code

  if(Build.VERSION.SDK_INT < 17) { android.provider.Settings.System.putInt(cr, android.provider.Settings.System.WIFI_SLEEP_POLICY, android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER); } else { android.provider.Settings.Global.putInt(cr, android.provider.Settings.Global.WIFI_SLEEP_POLICY, android.provider.Settings.Global.WIFI_SLEEP_POLICY_NEVER); } 

As a result, I started getting the following SecurityException on the Api-17 platform

  java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS 

Then I checked that the permission WRITE_SECURE_SETTINGS is intended only for system applications, and I could not compile my code with it, as if this permission was intended only for system applications.

So, I'm confused, there was a warning that I received earlier, it was wrong or something was wrong with my code, I want to make it compatible with API-17.

+8
android
source share
1 answer

Unfortunately, it is no longer possible to change this parameter from API-17, as it is deprecated .

As you said, WRITE_SECURE_SETTINGS permission WRITE_SECURE_SETTINGS granted only to system applications, so the best alternative is to ask the user to manually set this parameter from the Wi-Fi settings:

 startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); 
+1
source share

All Articles