0

I want to open GPS when Activity OnCreated. I'm using this code but it doesnt work. I get this error message:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=3818, uid=10084 Min SDK API 17 Max SDK API 23

I used to this Code:

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

    if(!provider.contains("gps")){ //if gps is disabled
        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")); 
        sendBroadcast(poke);
    }
}

And This Code I try them. But i get same issue.

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);`
TDG
  • 5,909
  • 3
  • 30
  • 51
Jack Marquies
  • 31
  • 1
  • 7
  • you cannot open gps from your code.. security issues and also violates google policy. The best you can do is add runtime permission to access gps on marshmallow and up devices – tk1505 Jul 22 '16 at 09:03

1 Answers1

0

Enable/Disable GPS programatically is not allowed because of security/privacy issues.

If you are referring to this thread, then this bug has been fixed.

You can use this to open the GPS settings page and let user enable or disable GPS manually,

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
Community
  • 1
  • 1
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33