setTestProviderLocation() does not trigger calling of onLocationChanged()

前端 未结 6 2056
野的像风
野的像风 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:24

    I have the same problem with this issue, I think the problem is TestProvider, so I set

    String mocLocationProvider= LocationManager.NETWORK_PROVIDER;

    and then it can call onLocationChanged().

    0 讨论(0)
  • 2020-12-10 08:26

    This is what worked for me

    locationManager.addTestProvider(mocLocationProvider, false, false,
                        false, false, true, true, true, 0, 5);
    locationManager.setTestProviderEnabled(mocLocationProvider, true);
    

    .

    Location mockLocation = new Location(mocLocationProvider); // a string
    mockLocation.setLatitude(location.getLatitude());  // double 
    mockLocation.setLongitude(location.getLongitude()); 
    mockLocation.setAltitude(location.getAltitude()); 
    mockLocation.setTime(System.currentTimeMillis()); 
    locationManager.setTestProviderLocation( mocLocationProvider, mockLocation); 
    

    .

    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">
    

    Also in the android phone settings make sure you have the "Allow mock locations" checkbox ticked

    0 讨论(0)
  • 2020-12-10 08:35

    I had the same problem. Couldn't resolve it and finally I gave up and added a call to onLocationChanged( location) after locationManager.setTestProviderEnabled( PROVIDER, location) manually. It's not pretty but works.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-10 08:42

    I ran into a similar problem. Regardless of what I tried, I could never get onLocationChanged() to be automatically called. Eventually, I gave up and created a method that both set the location with setTestProviderLocation() and directly called onLocationChanged(). It's not a perfect solution, but it worked for my needs.

    0 讨论(0)
  • 2020-12-10 08:43

    After adding and enabling your Provider just use this line to get the updates :
    locationManager.requestLocationUpdates(MOCK_PROVIDER, 0, 0, this);

    I recommend to use a method like this on activity start-up :

        private void enableProvider() {
            
            try {
                locationManager.addTestProvider(
                        MOCK_PROVIDER,
                        false,
                        false,
                        false,
                        false,
                        true,
                        true,
                        true,
                        0,
                        5
                );
            } catch (IllegalArgumentException | SecurityException e) {
                Log.w(TAG, "addTestProvider" + e.getMessage());
            }
            try {
                locationManager.setTestProviderEnabled(MOCK_PROVIDER, true);
                locationManager.requestLocationUpdates(MOCK_PROVIDER, 0, 0, this); <-- Update Request
            } catch (IllegalArgumentException | SecurityException e) {
                Log.w(TAG, "setTestProviderEnabled" + e.getMessage());
            }
        }
    

    Handle the exceptions as well.

    0 讨论(0)
提交回复
热议问题