Can't Switch GPS On/Off and Wireless Location Settings through System.secure

…衆ロ難τιáo~ 提交于 2020-01-05 05:25:10

问题


I want switch Location Providers On and Off, that is GPS and Wireless Location

I added permission in manifest

my code to change wireless location settings is...

Settings.Secure.setLocationProviderEnabled(context.getContentResolver(), provider, true);

whenever I run this code logcat shows error

logcat out put

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

I searched about this, many person says

The WRITE_SECURE_SETTINGS permission is not available to apps which aren't part of the firmware because secure settings are designed to be secured against modification by third party apps

Is it true, if yes I need any other way to achieve this,

if no then how to use this, is there any mistake in my code...

thanks in Advance

*Note: * I defined this method in different class file, and call this from SERVICE


回答1:


Android Settings.Secure.setLocationProviderEnabled is not allow directly calling the Settings from any other application.so try this way to check or enable gps:

private void isGPSEnable() {
        String str = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        Log.v("GPS", str);
        if (str != null) {
            return str.contains("gps");
        }
        else{
            return false;
        }
    }

an for enable/disable GPS :

private void turnGPSOnoff(){
     try
     {
     String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

     if(!provider.contains("gps")){
         final Intent poke = new Intent();
         poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
         poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
         poke.setData(Uri.parse("3"));  //SET 3 for gps,3 for bluthooth
         sendBroadcast(poke);
     }
     }
     catch(Exception e)
     {
      Log.d("Location", " exception thrown in enabling GPS "+e);
     }
 }


来源:https://stackoverflow.com/questions/11257888/cant-switch-gps-on-off-and-wireless-location-settings-through-system-secure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!