Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocationClient - Mock Locations

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")
like image 378
Francesco verheye Avatar asked Mar 19 '23 11:03

Francesco verheye


1 Answers

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.

like image 195
Warpzit Avatar answered Mar 28 '23 03:03

Warpzit