0
我假設getLastKnownLocation在爲同一提供者調用clearTestProviderLocation後爲給定提供者返回null。爲什麼getLastKnownLocation(提供者)在clearTestProviderLocation(提供者)後不返回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]
我缺少什麼?
但你錯過了一些東西;我確實爲'TEST'提供者設置了位置(沒有更多的嘗試),因此在調用'clearTestProviderLocation(「TEST」)'後,測試提供者必須有零位。結果,'getLastKnownLocation(「TEST」)'必須返回null – Blackkara