I have an Android app that gets location:
private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(120000);
mLocationRequest.setFastestInterval(60000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
return mLocationRequest;
}
private GoogleApiClient getLocationApiClient(){
return new GoogleApiClient.Builder(App.instance)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
...
apiClient = getLocationApiClient();
apiClient.connect();
@Override
public void onConnected(@Nullable Bundle bundle) {
...
LocationRequest locationRequest = createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, new LocationListener() {
@Override
public void onLocationChanged(Location newLocation) {
//***THIS IS NEVER CALLED ON EMULATOR***
}
});
}
When running on device (Galaxy S3, Android 4.4.4) there is no problem at all. When running on emulator (Android Studio default qemu, Android 7.1, x86-64) I'm not getting location on my app. onConnected
is called, I can even read the last location, though I won't get any location updates (requestLocationUpdates
completion never called).
I've:
- Added
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
to manifest (in addition to coarse and fine location). - Tried changing Google location settings on emulator (high accuracy, battery saving, device only)
- Tried setting location from Emulator's GUI.
- Tried turning emulator's "Use detected ADB location" option on and off.
- Tried
adb -s emulator-5555 emu geo fix 12.34 56.78
(command works, keep reading to see why)
I still can't get my app to get a location update. I've tried the emulator's built-in Google Maps and it gets location updates perfectly, I can see current position on map immediately change when I send different coordinates through geo fix.
But my app is completely unaware of location updates. I've tried waiting at least 2 minutes (my location request interval) before sending another coordinate too. What am I doing wrong?