0

I'm unable to send mock location on my real device. I've enabled Mock location in settings and disabled network location in Location settings.

Manifest:

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

Here is the code:

private static final String PROVIDER_NAME = "flp";
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.setTestProviderEnabled(PROVIDER_NAME, true);
locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, 1000, 1, new LocationListener() {
                            @Override
                            public void onLocationChanged(Location location) {

                            }

                            @Override
                            public void onStatusChanged(String provider, int status, Bundle extras) {

                            }

                            @Override
                            public void onProviderEnabled(String provider) {

                            }

                            @Override
                            public void onProviderDisabled(String provider) {

                            }
                        });

                mockLocation(12.333, 13.444);

And this method:

public void mockLocation(double lat, double lng) {
        // Create a new Location
        Location newLocation = new Location(PROVIDER_NAME);
        newLocation.setLatitude(lat);
        newLocation.setLongitude(lng);
        newLocation.setAccuracy(3.0f);
        newLocation.setTime(1);
        locationManager.setTestProviderLocation(PROVIDER_NAME, newLocation);
    }

Why is onLocationChanged() not called?

Omar
  • 7,835
  • 14
  • 62
  • 108

1 Answers1

1

This is what worked for me

locationManager.addTestProvider(mocLocationProvider, false, false,
                    false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);


Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(location.getLatitude());  // double 
mockLocation.setLongitude(location.getLongitude()); 
mockLocation.setAltitude(location.getAltitude()); 
mockLocation.setTime(System.currentTimeMillis()); 
locationManager.setTestProviderLocation( mocLocationProvider, mockLocation); 



<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">

Also in the android phone settings make sure you have the "Allow mock locations" checkbox ticked.

Source

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74