6

Possible Duplicate:
Enable GPS programatically like Tasker

I'm a beginner android developer.

I want enable and disable GPS with a click of a button, without running this intent:

intente in = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);   

startActivity(in);

that brings me in the setting menu.

Is there a method to enable or disable GPS as in the android "Power Control" widget?

Thanks

EDIT:

The widget that I said is in the android system (version 1.6 on forward). There is an application (siriusapplications.com/powercontrolplus) that make want I want do, and extend the android power control.. my question is: how this persor can manage this?? –

Community
  • 1
  • 1
mist3r0
  • 61
  • 1
  • 1
  • 2

2 Answers2

3

I don't think there is a way to do so. See this question. You can also find a nice example here. Basically what you do is test if GPS is enabled and then navigate user to the appropriate settings page.

The widget you are referring to probably has direct access to the GPS driver. This means that will only work in the device at hand (or in a single family of devices).

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • The widget that I said is in the android system (version 1.6 on forward). There is an application (http://www.siriusapplications.com/powercontrolplus/) that make want I want do, and extend the android power control.. my question is: how this persor can manage this?? – mist3r0 Oct 25 '10 at 14:04
  • Since the widget is in the android system it can actually use native code to control the GPS. As for the application, have you tried it out? Does it actually enable/disable the GPS or it just gives you a shortcut? This will be very interesting to know. – kgiannakakis Oct 25 '10 at 14:35
  • 1
    I've see that this software (http://www.siriusapplications.com/powercontrolplus/) is very similar at the Power Control windget of the android system. For this reason there is a mode for make this!! now maybe I have resolved my problem, I write this code: boolean gpsStatus = locmanager.isProviderEnabled(LocationManager.GPS_PROVIDER); if(!gpsStatus){ Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "network,gps"); } – mist3r0 Oct 25 '10 at 17:05
  • When execute this code the system require tow type of permission, android.permission.WRITE_SECURE_SETTINGS and android.permission.WRITE_SETTINGS but after I've put this mermissions in my manfest, I've always the folows excemtion: java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS. Can you help me?? – mist3r0 Oct 25 '10 at 17:05
  • @kgiannakakis -1 because **this can be done up to 2.2 (sdk 8)**. For details, see [Enable GPS programatically like Tasker](http://stackoverflow.com/a/5305835/383414) – Richard Le Mesurier Jun 20 '12 at 14:36
2

the GPS can be toggled by exploiting a bug in the power manager widget. see this question.

Ben H
  • 3,855
  • 1
  • 26
  • 33
  • +1 yes, **this can be done up to 2.2 (sdk 8)**. For details, see [Enable GPS programatically like Tasker](http://stackoverflow.com/a/5305835/383414) – Richard Le Mesurier Jun 20 '12 at 14:37