How to SET Android Mock GPS location?

依然范特西╮ 提交于 2019-12-04 11:13:46

you must set first:

allow mock location in setting

second set permissions:

ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION ACCESS_MOCK_LOCATION

third use this method :

setMock(some_lat,some_lon,some accuracy);

private void setMock(double latitude, double longitude, float accuracy) {
 locMgr.addTestProvider (LocationManager.GPS_PROVIDER,
            "requiresNetwork" == "",
            "requiresSatellite" == "",
            "requiresCell" == "",
            "hasMonetaryCost" == "",
            "supportsAltitude" == "",
            "supportsSpeed" == "",
            "supportsBearing" == "",
            android.location.Criteria.POWER_LOW,
            android.location.Criteria.ACCURACY_FINE);

    Location newLocation = new Location(LocationManager.GPS_PROVIDER);

    newLocation.setLatitude(latitude);
    newLocation.setLongitude(longitude);
    newLocation.setAccuracy(accuracy);
    newLocation.setAltitude(0);
    newLocation.setAccuracy(500);
    newLocation.setTime(System.currentTimeMillis());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    }
    locMgr.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

    locMgr.setTestProviderStatus(LocationManager.GPS_PROVIDER,
            LocationProvider.AVAILABLE,
            null,System.currentTimeMillis());

    locMgr.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);}

You must set accuracy and ElapsedRealtimeNanos values

location.setAccuracy(...);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}

Otherwise it throws IllegalArgumentException.

@throws IllegalArgumentException if the location is incomplete

UPDATE

If you mock Network provider

String mocLocationProvider = LocationManager.NETWORK_PROVIDER;

then you will receive mock location through Network provider,

LocationListener listener = new LocationListener(){
    ...
    void onLocationChanged(Location location){

    }
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, ..., listener);

And whenever you invoke setTestProviderLocation method, then onLocationChanged callback will be triggered !

from @Blackkara's answer and comments, this is the final code, and do not forget to set the location mode to device only(GPS)

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy( Criteria.ACCURACY_FINE );

    String mocLocationProvider = LocationManager.GPS_PROVIDER;//lm.getBestProvider( criteria, true );

    if ( mocLocationProvider == null ) {
        Toast.makeText(getApplicationContext(), "No location provider found!", Toast.LENGTH_SHORT).show();
        return;
    }
    lm.addTestProvider(mocLocationProvider, false, false,
            false, false, true, true, true, 0, 5);
    lm.setTestProviderEnabled(mocLocationProvider, true);

    Location loc = new Location(mocLocationProvider);
    Location mockLocation = new Location(mocLocationProvider); // a string
    mockLocation.setLatitude(-26.902038);  // double
    mockLocation.setLongitude(-48.671337);
    mockLocation.setAltitude(loc.getAltitude());
    mockLocation.setTime(System.currentTimeMillis());
    mockLocation.setAccuracy(1);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    }
    lm.setTestProviderLocation( mocLocationProvider, mockLocation);
    Toast.makeText(getApplicationContext(), "Working", Toast.LENGTH_SHORT).show();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!