Android LocationListener goes off when phone sleeps

I set an alarm with the RTC_WAKEUP flag to fire the IntentService every 30 seconds to send location updates to the server. I am planning to change the flag to RTC so that it doesn't wake up the phone and just start up when another process wakes up the phone. If I stay the registered LocationListener, will it still listen for location updates while sleeping?

+1


source to share


2 answers


Yes. The workplace service has its own tracking blocker. However, the best approach is to manually set the proper tracking lock on your broadcast receiver. Please consider some optimization - sending data over the network every 30 seconds will drain your battery.



+2


source


You have several problems here.

I set an alarm with the RTC_WAKEUP flag to fire the IntentService every 30 seconds to send location updates to the server.

First, you might not even get your first fix within 30 seconds, especially if you are using GPS. You should take into account that you may never receive a fix (for example, the user is in an underground location).

Second, please allow the user to customize this drawing by enabling the "I will load data manually" option. As @piotrpo points out, this is a significant battery drain. In fact, if you are using GPS, I doubt the battery will last more than two hours.



Third, IntentService

it won't work in this case, because it IntentService

will be closed before your patch arrives. At best, you'll be leaking memory. In the worst case, you won't get your fix because Android is terminating your process.

The best solution for doing background location checking is to use regular Service

rather than IntentService

. The Regular Service will log LocationListener

in onStartCommand()

and also arrange for a timeout notification (e.g. AlarmManager

and set()

) if a fix is ​​not available. When the fix arrives, run AsyncTask

to download. When it completes, AsyncTask

or if it times out and you haven't received a fix, deregister the listener and call stopSelf()

to close the service. Along the way, you'll need to maintain your own WakeLock

to keep the device from waking up while this is all happening.

For an example of most of these (minus some of the server load), see LocationPoller

.

If you're dead when it happens every 30 seconds or so, you don't have to worry at all AlarmManager

. You will have to have eternal ministry, working all the time, constant WakeLock

and constant LocationListener

. When patches arrive onLocationChanged()

, download them if they are more than 30 seconds away from the previous one. And, don't forget to wear a flame retardant suit when you release the app, as those who launch it may not like it very much.

+2


source







All Articles