setTestProviderLocation() does not trigger calling of onLocationChanged()

前端 未结 6 2081
野的像风
野的像风 2020-12-10 07:42

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:



        
6条回答
  •  感情败类
    2020-12-10 08:37

    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.

提交回复
热议问题