I am in the process of creating a custom Phonegap plugin for Android that monitors location both when the app is in the foreground and when it is backgrounded. Google documentation on using the FusedLocationProviderAPI is remarkably clear. The process I have worked out thus far is as follows
Ensure that the API is available
GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(ctxt); return (code == ConnectionResult.SUCCESS);
Define a LocationListener with assigned callbacks to handle the results retured by the
requestLocationUpdates
method.- Create a LocationRequest
Here is where things become slightly unclear
setInterval
- the interval at which the app wants location updatessetFastestInterval
- the interval at which it will consume updates if available.setSmallestDistance
&setPriorty
- clear enoughsetNumUpdates
- how this works is not clear to me. Reading between the lines I am assuming that if I usesetInterval(60000)
andsetNumUpdates(1000)
the system will keep sending back location updates for the next 6000 minutes or until such time as the app is backgrounded/shutdown or I/the user cancels location updates.
But then this begs the question - what does the app need to do to be a good citizen. I am assuming that would have to be something like this
- Record the
PendingResult
being returned by therequestLocationUpdates
call. - Detect when the onPause event occurs
- call
PendingResultt.cancel()
prior to letting the app go to the background
I'd be much obliged if someone could comment on the correctness of this workflow.
A related issue - The documentation for PendingResult
states
It is the responsibility of the caller or callback receiver to release any resources associated with the returned result.
It is not clear to me what resources they are talking about here. The LocationListener.onLocationChanged
event returns a Location object which I assume will be garbage collected when it goes out of scope. Presumably the PendingResult
being returned by requestLocationUpdates
should be canceled and then set to null when the app goes to the background. Is there anything else one needs to do by way of releasing resources?
A few hours later
I created two versions of my test app
- App 1:Sets up the LocationRequest with
setNumUpdates(10000)
. Pops up toasts on location change in the form App 1:Location is... - App 2:Sets up the LocationRequest with
setNumUpdates(1)
. Pops up toasts on location change in the form App 2`:Location is...
I had the two apps running simultaneously and simulated position changes with the help of a really neat little app called FakeGPS. Both App1 and App2 provided me with an update when I did my first fake location change. However, all subsequent location changes were reported only by App 1.
By inference then setNumUpdates
provides a mechanism for polling for updates periodically. What is slightly confusing is that the updates continue even after the app is backgrounded - though I assume that this is largely because it is at the mercy of the OS which will kill it when it deems fit.
However, all of the above is based on empirical testing. I find surprisingly little on the setNumUpdates
setting.