Search code examples
androidasynchronouswakelocklocationlistenercommonsware-cwac

How to wait for onLocationChanged() in LocationListener in the same place that invoked requestLocationUpdates()?


I'm writing a GPS tracking app. I'm using CommonsWare WakefulIntentService and AlarmReceiver to schedule fixing locations from GPS. The problem is that I must use doWakefulWork() method is meant to be used with synchronous blocking processes where what I have is an asynchronous non-blocking method. So how should I fix GPS locations using onLocationChanged() in LocationListener and requestLocationUpdates() from doWakefulWork() method?

Solved:

I just found out that this functionality already has been implemented in LocationPoller.


Solution

  • I'm using CommonsWare WakefulIntentService and AlarmReceiver to schedule fixing locations from GPS.

    That will not work well.

    So how should I fix GPS locations using onLocationChanged() in LocationListener and requestLocationUpdates() from doWakefulWork() method?

    Ideally, you wouldn't. IntentService is designed for making synchronous stuff (e.g., Web transactions, database transactions) synchronous. It is not designed for making asynchronous stuff even more asynchronous (a-asynchronous?)..

    I just found out that this functionality already has been implemented in LocationPoller.

    Whether you use LocationPoller, the fork, or something derived from it, using a Service for intrinsically asynchronous work is a better plan.