I want to use an app that tracks my location every one hour. The app runs in background and I wanted to ask if there is a way to make the GPS
to be turned on Every time the app asks for location from background and after it gets the location, it makes the GPS
turned off in order to save battery life.
I tried to use tasker
but it works only when I open and close the app. It doesn't function when the app asks for location from the background.
Thanks in advance!
3 Answers
To save battery, you could use requestSingleUpdate on LocationManager
and set an alarm every hour with setInexactRepeating on AlarmManager
.

- 7,495
- 3
- 37
- 55
-
Is this the best method? I need it to be synced with the app so it will turn on GPS when the app requests and I didn't see an option for this in the link you've provided, Thanks! – Ofirk Jun 03 '16 at 11:07
-
@Ofirk, sorry it's not very clear, do you want to "enable" GPS programmatically, if it's disabled in android settings ? – LaurentY Jun 03 '16 at 12:12
-
I want that every time the tracking app (which runs in background) asks for location the phone will recognize that, enable GPS and disable it right after the app gets the location. – Ofirk Jun 03 '16 at 20:11
-
If user enabled GPS in android settings and no applications request location, then GPS is in "sleep mode". As soon as 1 app request for location then GPS start to get a location. – LaurentY Jun 06 '16 at 07:18
there is a common misconception about the GPS
setting, that having it enabled, drains battery.
It is not necessarily true.
When WiFi
is turned on, it scans for signal every once in a while, or is transmitting data (also in the bacground, if not disabled in the settings)
However with GPS
, as mentioned by LaurentY
, situation is entirely different, as location is being checked only if requested, so it makes no sense to have it disabled and then toggle it every time an app needs it. Especially on Android 6.0 where you can manage per-app permissions (disable GPS
access to all non-essential apps and allow only that one).
you can also have Tasker check your location at predefined time intervals, not having to use that app you mentioned before

- 487
- 3
- 15
-
Really good point this would be interesting to see if anything set up ans documented, re tests, results... And method... Adding to list (todos: taskertestprofiles ) – Jonny Apr 04 '21 at 21:27
Unfortunatelly you can't disable/enable GPS programatically without user accept. If you want to enable GPS programatically, User should be notified about that and he should accept GPS enabling. I think in your choice you can just set minimum required time to get User position using requestLocationUpdates
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 5, locationListener);
In this choice GPS will be used each 60000 milliseconds , but it would be enabled all time. In this way you will save your bettery life.

- 550
- 5
- 15
-
Thanks! Can you specify how to enable GPS programatically with asking user acceptance? – KasparTr Sep 26 '16 at 10:51
-
see this link http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ – Däñish Shärmà Sep 27 '16 at 04:59