Dayerman
Dayerman

Reputation: 4003

Keep locationListener active between activities

I start an activity in my class Locator.java which starts a locacionListener Service. That activity writes positions to a database. I wonder if when i move to next screens(activities), positions will keep writing to the database, or I would need to go back to Locator.java to keep writting. The same when I minimize the application.

I have thouhgt about using a Service, but I want to be sure I need it.

Upvotes: 1

Views: 403

Answers (1)

ddewaele
ddewaele

Reputation: 22603

As long as the service is started, and providing sufficient resources are available on the device, the service will continue to run in the background. Switching activities, minimizing or closing the app doesn't change that. It's up to you to formally stop the service.

The business logic for capturing location updates and storing them in the database should be implemented in the service however, and not in the activity. (The activity can be used to control the state of the service (stop-start), and to display data from the database, but the bulk of your location update / storing in database logic should be triggered from the service.

You can check the running services on your android device (Settings - Applications - Running Services). You should see your service, and the amount of time it has been running.

Depending on your requirements, a simple service will not cut it. If you for example you also want to capture location updates and store them in a database while the device is sleeping, you'll need to look at WakefullintentService

Upvotes: 2

Related Questions