Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WIFI_SLEEP_POLICY_NEVER how to set in API-17?

Tags:

android

I was using following code , my target 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 platform API-17 I get warnigns in the logcat,

Setting wifi_sleep_policy has moved from android.provider.Settings.System to android.provider.Settings.Global, value is unchanged.

So what I did after that was set my project target to APi-17 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 following SecurityException in platform Api-17

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

Then I checked that WRITE_SECURE_SETTINGS permission is only for system apps and I was not able to compile my code with it, as if this permission is for system apps only.

So I am confused was the warning that I got earlier was wrong or is there anything wrong with my code, I want to make it compatible it with API-17.

like image 628
Ahmed Avatar asked Dec 27 '12 22:12

Ahmed


1 Answers

Unfortunately it is not possible to change this setting anymore from API-17 onward since it has been deprecated.

As you said, the WRITE_SECURE_SETTINGS permission is only granted to system apps and so the best alternative is to ask the user to manually set this option from the wifi settings:

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
like image 142
Nachi Avatar answered Sep 27 '22 18:09

Nachi