The device's location can be easily mocked using the Device.mockLocation method by providing a GeoLocation as a parameter. Information about how to get a Device instance can be found here.
- You will only need latitude and longtitude values to create a
GeoLocationobject. Theproviderparameter is not required and defaults togps:
GeoLocation mockLocation = new GeoLocation(latitude, longitude, provider);A list of all currently supported providers (by API level) is available in the LocationManager documentation. You can either use the provider's string value as a GeoLocation parameter or import the LocationManager class and use its constants like this: LocationManager.GPS_PROVIDER.
- You can set additional properties of the
GeoLocationobject by using itssetmethods, for example:
mockLocation.setAltitude(altitude);- Once you have a
GeoLocationinstance ready, just call themockLocationmethod and use the location as a parameter:
device.mockLocation(mockLocation);The device's location should now be being mocked.
- To disable the location mocking for a specific provider use
disableMockLocation:
device.disableMockLocation(provider);