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:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
to manifest (in addition to coarse and fine location).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?
The fused location provider is a location API in Google Play services that intelligently combines different signals to provide the location information that your app needs.
How to configure GPS location “Google Map” on your Android Studio Emulator. First, run your Android Emulator and click on the three dots on the menus beside. On the left pane, select Location and change the coordinates according to your location. Press the Send button, changes will immediately take effect.
There is no official API in iOS or Android to detect an emulator. Therefore, several proprietary checks have to be done by the RASP system.
I have run into the same problem on the emulator. The only fix I have found is to use PRIORITY_HIGH_ACCURACY.
Explanation: It is the only option that will force android to turn on the GPS chip (virtual chip in the case of the emulator). On a real device this is not an issue because the phone can use cell reception and wifi to figure out an approximate location. On the emulator there is no cell reception and the wifi is abstracted as just a network connection. So the emulator can pretty much only use the GPS for location.
If you really do not want to use PRIORITY_HIGH_ACCURACY in your actual application you could create (or find an example app) for FusedLocationApi with the accuracy set to PRIORITY_HIGH_ACCURACY. Run that before you run your actual app (PRIORITY_BALANCED_POWER_ACCURACY) and there will be a last known location available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With