Testing with Mock Location Data in Android

The DDMS tool can be used to push out test location during testing. However, it has two serious limitations:

  1. DDMS sets location for GPS location provider only. If your application uses network provider then you are out of luck.
  2. DDMS can set location for an emulator device only. You can not do testing using a real device.

If you need to test with real device or using the network location provider, you will need to develop a mock provider within the application. A mock provider can represent any location provider – network or GPS. Writing a mock provider itself is easy. Just be careful about removing the feature before publishing your application.

In this article, we will see how to create a mock location provider.

First, we will develop a class that will encapsulate the details:

public class MockLocationProvider {
  String providerName;
  Context ctx;

  public MockLocationProvider(String name, Context ctx) {
    this.providerName = name;
    this.ctx = ctx;

    LocationManager lm = (LocationManager) ctx.getSystemService(
    lm.addTestProvider(providerName, false, false, false, false, false, 
      true, true, 0, 5);
    lm.setTestProviderEnabled(providerName, true);

  public void pushLocation(double lat, double lon) {
    LocationManager lm = (LocationManager) ctx.getSystemService(

    Location mockLocation = new Location(providerName);
    lm.setTestProviderLocation(providerName, mockLocation);

  public void shutdown() {
    LocationManager lm = (LocationManager) ctx.getSystemService(

A brief description of the MockLocationProvider class may be helpful:

  • The constructor takes the name of the location provider that this mock provider will replace. For example, LocationManager.GPS_PROVIDER. The calls to the addTestProvider() and setTestProviderEnabled() tells the LocationManager that the given provider will be replaced by mock data.
  • The pushLocation() method supplies mock location data for a given provider.

Any activity or service can easily use the class. Here, we are replacing the network provider with a mock one.

public class MainActivity extends Activity {
  MockLocationProvider mock;
  public void onCreate(Bundle savedInstanceState) {

    mock = new MockLocationProvider(LocationManager.NETWORK_PROVIDER, this);

    //Set test location
    mock.pushLocation(-12.34, 23.45);

    LocationManager locMgr = (LocationManager) 
    LocationListener lis = new LocationListener() {
      public void onLocationChanged(Location location) {
          //You will get the mock location

      LocationManager.NETWORK_PROVIDER, 1000, 1, lis);

  protected void onDestroy() {

Setting up Security

For mock location to work, certain permissions have to be set.

You will need to request the android.permission.ACCESS_MOCK_LOCATION permission, in addition to any other permission.

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

Finally, you will need to enable mock locations for the device using Settings > Developer options > Allow mock locations.

Disabling Mock Location

It is important that you hide the mock location provider business from the release build. A good way to do that is to enable mock location only if the application is being run in debug mode. In your code, check if debuggable flag is set:

if ((getApplication().getApplicationInfo().flags & 
    ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
    mock = new MockLocationProvider(
        LocationManager.NETWORK_PROVIDER, this);

    //Set test location
    mock.pushLocation(-12.34, 23.45);

When you test the app using USB debugging or using the emulator, the debug flag is set automatically.


10 thoughts on “Testing with Mock Location Data in Android

  1. hi bibhas2 ,thanks for you article that help me a lot.

    It ok with the provider name “testProvider” and GPS_PROVIDER,

    butwhen i try to mock the NETWORK_PROVIDER i got a “LLLegalArgumentException:Provider “network” unknown”,

    What’s the problem with this provider in my device?

    • I suggest two things. First, make sure that network location provider is enabled for the device:

      if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

      Second, try testing with a real device. Some have reported problems with emulator and network provider.

  2. Hey Man , its a really nice tutorial but unfortunately its not working for other applications , I mean the other application (like google maps etc ) are still showing my location in India whereas i set my location coordinates to that of new-york city

  3. Thanx a lot for the tutorial. I made some progress in simulating the GPS.

    What I see is that only 1 location update is received, although I send many mockLocations. The mocklocations are sent each 2 seconds for a number of minutes. The distance is also much more than the threshold.

    Can you please help?

  4. Already found the solution: although I send new mock location (updates) every 2 seconds, at the receiving end I should allow locations updates every 100 ms. Putting this last value at 200 ms filters new location updates. Why? I don’t know.

  5. Hello Bibhas2
    This method is working only for GPS_PROVIDER not for high accuracy mode and battery saving mode. I have put all checks, to know wether the provider is enable or not, mock location is enable or not etc. I have googled also and suspect wether this method can mock only gps coordinates or other location too.

  6. Doesn’t work, added:

    But still doesn’t work:
    Caused by: java.lang.IllegalArgumentException: Incomplete location object, missing timestamp or accuracy? Location[network -12,340000,23,450000 acc=1 et=?!? alt=0.0 bear=1.0]
    at android.location.LocationManager.setTestProviderLocation(LocationManager.java:1274)

    • Heres a fix,add it in pushLocation()

      Method locationJellyBeanFixMethod = null;
      try {
      locationJellyBeanFixMethod =Location.class.getMethod(“makeComplete”);
      } catch (NoSuchMethodException e) {
      if (locationJellyBeanFixMethod != null) {
      try {
      } catch (IllegalAccessException e) {

      } catch (InvocationTargetException e) {

      • This is a very old blog post. I do not have the time to verify if your code fixes any problems. But hopefully others will find your comment helpful. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.