I\'m trying to get this bit of code to work:
Testing GPS in Android
The problem is, when I run the test, onLocationChanged() is never called:
I had the same issue and could get it to work. I'll share it here in case it can help anyone else.
The thing to consider is that any call to setTestProviderLocation
will invoke any listener registered on the mock providers at the time of the invocation. So if you call setTestProviderLocation
and right after that call requestLocationUpdates
, the onLocationChanged
callback never gets called.
My problem was that I had these two (setTestProviderLocation
and requestLocationUpdates
) in two different threads and requestLocationUpdates
was called after, so I never got a callback to onLocationChanged
. If your program is multi-threaded (which very likely is) make sure setTestProviderLocation
is called after requestLocationUpdates
.
Also I noticed for any call to setTestProviderLocation
, onLocationChanged
will be called once (approximately). So you can put setTestProviderLocation
in a loop to simulate a series of calls to onLocationChanged
. This way you can simulate a path as well.