LocationClient - Mock Locations

android, google-play-services, location-client, locationlistener

Solution

Okay so you gotta update your Locations with `setTime()` and `setElapsedRealtimeNanos()`.

A complete create method for a location will look something like following:

@SuppressLint("NewApi")
public Location createLocation(double lat, double lng, float accuracy) {
    // Create a new Location
    Location newLocation = new Location(PROVIDER);
    newLocation.setLatitude(lat);
    newLocation.setLongitude(lng);
    newLocation.setAccuracy(accuracy);
    newLocation.setTime(System.currentTimeMillis());

    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        newLocation.setElapsedRealtimeNanos(
             SystemClock.elapsedRealtimeNanos());
    }
    return newLocation;
}

This has been tested to work with Nexus 5.

Problem

I'm using the LocationClient which works good. Now I'm trying to create mock locations (setMockMode(true) + setMockLocation(mockLoc). But onLocationChanged of my LocationListener isn't called. What can be the problem? I followed this: http://developer.android.com/training/location/location-testing.html Steps: - connect - requestLocationUpdates - setMockMode true - setMockLocation (provider = "flp")

Original source