Jahid Shohel
Jahid Shohel

Reputation: 1485

Android LocationListener stop receiving location updates

I have registered a location listener, which will receive location updates using NETWORK_PROVIDER. The listener receives location updates if the activity is in foreground. If I leave the app, after some time it stops receiving location updates. Which is a bit weird... isn't it supposed to receive location update even if none of the activity is on the stack? The listener starts receiving location updates again if I go back to my app.

Or am I doing something wrong? What programming model is suggested to receive location updates even if none of my activity is not active (neither on foreground, nor on stack)?

There are some other way to receive location updates (using pending intent), but those do not trigger if I do not turn on GPS.

Upvotes: 1

Views: 1855

Answers (1)

aromero
aromero

Reputation: 25761

The activity in the background will be eventually destroyed, you don't have any guarantee that once stopped it will continue receiving location updates. Activities in Android are not supposed to be used like this.

You can use a service with a wake lock as suggested, but keeping the device awake will quickly drain your battery.

I recommend you this talk by Reto Meier (IO 2011), where he talks about different strategies to have a fresh location. Here is the code.

Upvotes: 2

Related Questions