سؤال

I am writing a program that, among many things, uses GPS data from a secondary source. I have been implementing this using the Mock Location API:

LocationManager locationManager = ...

locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);

locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderStatus(LocationManager.GPS_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());

Location location = ...
location.setLatitude(...);
location.setLongitude(...);
... // etc.
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);

The good news: this works great! There was partying, confetti, and a marching band. The bad news: if I decide I want to use the primary GPS source again, removing this mock seems not to work; instead I get no new GPS updates ever. It's as if the primary GPS source was disabled by adding the mock and removing the mock never turned it back on.

locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, false);
locationManager.clearTestProviderEnabled(LocationManager.GPS_PROVIDER);
locationManager.clearTestProviderLocation(LocationManager.GPS_PROVIDER);
locationManager.removeTestProvider(LocationManager.GPS_PROVIDER);

Is there another step that needs to be performed in order to restore the primary GPS source?

هل كانت مفيدة؟

المحلول

The problem turned out to be a third-party component misbehaving. I wish I could post code saying what changed, but I personally changed literally nothing, so the actual problem was nothing to do with the Android API.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top