Android Question Persist GPS

swabygw

Active Member
Licensed User
In an app with several Activity's, GPS is turned on in Main in Activity_Resume and turned off in Activity_Pause. I'd like to keep the GPS on when going to another Activity within the application (and during an orientation change), but turn off when killing the application or clicking a button to turn it off. From the lifecycle tutorial, these are the four conditions when Activity_Pause is called, and are occurring in the app:

1. A different activity was started.
2. The Home button was pressed
3. A configuration changed event was raised (orientation changed for example).
4. The Back button was pressed.

Is there a way to get GPS to persist if #1 and/or #3 occur? (really, to only turn off if user exits app or clicks a button to turn it off)
 

MaFu

Well-Known Member
Licensed User
I one of my apps i had the same problem.
I put the GPS code in a service. When my app goes to background i start a timer and after one minute the timer switch GPS off. If i go back to my app in less than a minute the timer is cancelled and GPS stays on.
 
Upvote 0

swabygw

Active Member
Licensed User
Thanks for your advice. I've been playing around with this example: https://www.b4x.com/android/forum/threads/gps-tutorial.6592/#content, and reading up here: https://www.b4x.com/android/forum/threads/service-modules.7542/, and I think that what I want to do is comment out the line to stop the GPS in the Activity_Pause, and call StopService(MyGpsService) programmatically when I want to kill GPS. This GPS example has (finally) given me a good understanding of how Services work. I can think of a dozen things I'd like to do with them. :)
 
Upvote 0
Top