Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app can't access location on emulator using FusedLocationApi

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?

like image 700
Can Poyrazoğlu Avatar asked Jan 10 '17 01:01

Can Poyrazoğlu


People also ask

What is fused location on Android?

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 do I change the location of my Android phone emulator?

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.

Can apps detect emulators?

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.


1 Answers

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.

like image 151
startoftext Avatar answered Oct 20 '22 14:10

startoftext