0

I need to enable GPS programmatically so that I used following to code enable GPS:

            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);

But when i Used this code I got following Exception:

    01-25 12:11:37.218: W/ActivityManager(477): Permission denied: checkComponentPermission() reqUid=1000
    01-25 12:11:37.218: W/ActivityManager(477): Permission Denial: broadcasting Intent { cat=[android.intent.category.ALTERNATIVE] dat=3 cmp=com.android.settings/.widget.SettingsAppWidgetProvider } from com.gpstraking (pid=982, uid=10186) requires null due to receiver com.android.settings/com.android.settings.widget.SettingsAppWidgetProvider

What permission I have to give to avoid this exception and Enable GPS.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62

2 Answers2

1

Do you have below line in your manifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
0

Add this to the Manifest:

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"></uses-permission>
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226