Reputation: 1164
Apparently there are 2 ways of setting mock location in the Android ( currently playing with Android 11 )
One way using FusedLocationProviderClient:
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.setMockMode(true);
mFusedLocationClient.setMockLocation(createLocation(LAT, LNG, ACCURACY));
Another using LocationManager:
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.addTestProvider(providerName, false, false, false, false, false,
true, true, 1, 1);
lm.setTestProviderEnabled(providerName, true);
Location mockLocation = createLocation(LAT, LNG, ACCURACY);
lm.setTestProviderLocation(providerName, mockLocation);
Wondering what is the right way. For me second works fine, the first one in not ( still investigating ) But I wondering what is the RIGHT way? Also wondering why there are 2 distinct ways ?
Upvotes: 1
Views: 560
Reputation: 93542
The difference is LocationManager vs LocationServices.
LocationManager is the built in Android location functionality. It provides access to location via GPS or network identification. Every device has this.
LocationServices is a Google Play Services functionality. It main value is the "fused" provider that uses network, slight GPS use, and other sources of data to provide a location. It tends to be more accurate than network with less battery power than GPS (but not as accurate/precise as GPS). Only devices whose OEMs pay Google for access to Google Play Services have this (this is mainly an issue in Asia and Africa).
Two different systems, two different ways of mocking location. Your top one works if you're getting locations via Google Play Services, the bottom if via LocationManager. Use the one matching the way you get locations normally.
Upvotes: 1