setTestProviderLocation() does not trigger calling of onLocationChanged()

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

提交回复
热议问题