Skip to content Skip to sidebar Skip to footer

Use Mock Location Without Setting It In Settings

I am writing an App which makes use of the location mocking possibility in android. What I would like to achive is to mock my location without setting the 'allow mock locations' fl

Solution 1:

I have decompiled com.lexa.fakegps you mentioned in question, and what it do like this:

privateintsetMockLocationSettings() {
    intvalue = 1;
    try {
        value = Settings.Secure.getInt(getContentResolver(),
                Settings.Secure.ALLOW_MOCK_LOCATION);
        Settings.Secure.putInt(getContentResolver(),
                Settings.Secure.ALLOW_MOCK_LOCATION, 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    returnvalue;
}

privatevoidrestoreMockLocationSettings(int restore_value) {
    try {
        Settings.Secure.putInt(getContentResolver(),
                Settings.Secure.ALLOW_MOCK_LOCATION, restore_value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/* every time you mock location, you should use these code */intvalue = setMockLocationSettings();//toggle ALLOW_MOCK_LOCATION ontry {
    mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, fake_location);
} catch (SecurityException e) {
    e.printStackTrace();
} finally {
    restoreMockLocationSettings(value);//toggle ALLOW_MOCK_LOCATION off
}

Because the execution time of these code is very short, other apps can hardly detect the change of ALLOW_MOCK_LOCATION.

Also you need, 1. require root access to your device 2. move your app to /system/app or /system/priv-app

I tried it in my project and it works fine.

Solution 2:

You would require root access to your device to do that.

Solution 3:

Try this http://repo.xposed.info/module/com.brandonnalls.mockmocklocations

Some apps won't let you use them if "Allow mock locations" is turned on, even if you aren't mocking your location. This helps prevent apps from detecting that "Allow mock locations" is turned on.

Post a Comment for "Use Mock Location Without Setting It In Settings"