ravi152
ravi152

Reputation: 209

How to enable/disable GPS Programatically in Android 4.4?

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
getBaseContext().sendBroadcast(intent);`

I use this code for Enable GPS but it give me error like this.

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=1208, uid=10051

So anyone give me solution of that.

Upvotes: 3

Views: 11079

Answers (2)

karthik
karthik

Reputation: 219

public void turnGPSOn()
     {
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

     String provider = Settings.Secure.getString(ctx.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"));
     this.ctx.sendBroadcast(poke);
     }

Upvotes: 0

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

you never do that. it was bug in API. you can now give settings to user for choose using below intent:

startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

Upvotes: 5

Related Questions