为什么getLastKnownLocation(provider)在clearTestProviderLocation(provider)之后不返回null

时间:2016-11-16 14:33:37

标签: android mocking location

我假设在为同一个提供程序调用clearTestProviderLocation后,getLastKnownLocation确实为给定的提供程序返回null。

为什么?因为文档说明了clearTestProviderLocation;

  

删除与给定提供商关联的任何模拟位置。

public void test() throws SecurityException {
    String provider = "TEST";
    Location location = new Location(provider);
    location.setAccuracy(100);
    location.setTime(System.currentTimeMillis());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    }

    mLocationManager.addTestProvider(provider, false, false, 
        false, false, false ,false, false, 0, 0);

    mLocationManager.setTestProviderEnabled(provider, true);
    mLocationManager.setTestProviderLocation(provider, location);

    Location locationBefore = mLocationManager.getLastKnownLocation(provider);
    Log.d(TAG, locationBefore.toString());

    mLocationManager.clearTestProviderLocation(provider);

    Location locationAfter = mLocationManager.getLastKnownLocation(provider);
    Log.d(TAG, locationAfter.toString());
}

运行测试方法后,locationBefore和locationAfter相同!

和下面的日志一样,

Location[TEST 0.000000,0.000000 acc=100 et=+4d10h54m26s455ms mock]
Location[TEST 0.000000,0.000000 acc=100 et=+4d10h54m26s455ms mock]

我缺少什么?

1 个答案:

答案 0 :(得分:0)

getLastKnownLocation的{​​{3}}表示

  

如果当前禁用了提供程序,则返回null。

您需要首先禁用getLastKnownLocation的测试提供程序以返回null。