13

I need to be able to toggle the GPS receiver on and off, and WRITE_SECURE_SETTINGS is required to be able to access secure settings. I've searched around quite a bit, and every answer I saw pretty much said that no app outside of the system/firmware can get that permisssion.

However, that is simply untrue. There are several apps on the market that do exactly what I'm trying to (in regards to GPS), but there are a bunch more that have the WRITE_SECURE_SETTINGS permissions. For example:

Extended Controls

SwitchPro

Profile Flow

So, how can this be done?

amithgc
  • 6,167
  • 6
  • 29
  • 38
user496854
  • 6,461
  • 10
  • 47
  • 84

3 Answers3

26

I need to be able to toggle the GPS receiver on and off

For privacy reasons, if nothing else, enabling or disabling any sort of location-tracking needs to be solely in the hands of the user via trusted applications, not at the request of arbitrary third parties.

So, if you wish to enable and disable GPS, create your own firmware that does what you need and load that firmware on whatever devices you wish. Or, contribute your changes to existing firmware mods (e.g., Cyanogen).

WRITE_SECURE_SETTINGS is required to be able to access secure settings

Correct.

I've searched around quite a bit, and every answer I saw pretty much said that no app outside of the system/firmware can get that permisssion.

Correct.

However, that is simply untrue.

No, it's pretty true.

There are several apps on the market that do exactly what I'm trying to (in regards to GPS)

They found a security loophole. I will take steps to help ensure this hole gets fixed.

but there are a bunch more that have the WRITE_SECURE_SETTINGS permissions

No, there are a bunch who ask for them. You can ask for whatever permission you want. What you ask for is what shows up in these listings. What you get is a different story.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Also, FWIW, Extended Controls (which I purchased) does not directly enable or disable GPS, but rather takes you to the Location and Security Settings page to toggle it yourself. – CommonsWare Feb 17 '11 at 20:34
  • But in my app, the user would be selecting the option -- just trying to save a bunch of steps of taking them to settings first. Wouldn't it make more sense if there was an extra setting in "Location and Security" (with a sternly worded warning, of course) that allows extrenal apps to control GPS? That way a user can choose ease-of-use over security or vice versa. – user496854 Feb 17 '11 at 21:42
  • And I'm sorry about Extended Controls, I meant Power Controls plus. I've gone through a bunch of them over the last few weeks, and got the two mixed up. – user496854 Feb 17 '11 at 21:44
  • @user496854: I don't disagree with your proposed feature (user whitelist of apps that can enable/disable GPS), but it's not there, nor is there anything quite like that in Android today. Feel free to post an issue to http://b.android.com if you like with your proposed feature. With respect to Extended Controls, it just happened to be one I already owned and could test readily. I tried buying SwitchPro to see what that did, but the Android Market was acting flaky for me. – CommonsWare Feb 17 '11 at 21:55
  • @CommonsWare Can you clarify what you mean by "What you ask for is what shows up in these listings?" Are you talking about the Android App Permissions listing that shows up before installing the app? – jwir3 Mar 24 '14 at 16:20
  • @jwir3: Yes, or the equivalent piece in the Web-based Play Store UI. – CommonsWare Mar 24 '14 at 16:26
21

Try this adb command, this will give permission at application run time

adb shell pm grant package android.permission.WRITE_SECURE_SETTINGS

Step to follow:

  1. Connect your device (Make sure USB Debugging enabled)

  2. Execute above adb command

  3. Install apk

It worked for me...

Pradeep
  • 2,530
  • 26
  • 37
1

Just this. You can access the widget that every mobile have.

public void ativaGPS() {
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Intent intent = new Intent();
            intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            intent.setData(Uri.parse("3"));
            sendBroadcast(intent);
        }
keyser
  • 18,829
  • 16
  • 59
  • 101
Gabriel Augusto
  • 1,002
  • 11
  • 18