6

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" flag in the developer options.

I know that it is possible, because is works with this app: https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en

What I tried:

Generate an apk, move it to /system/app, do a reboot
And I also tried it with and without the ACCESS_MOCK_LOCATION permission in the manifest.

But it all lead to this exception:
RuntimeException: Unable to start Activity: SecurityException: Requires ACCESS_MOCK_LOCATION secure setting

lukassteiner
  • 787
  • 1
  • 6
  • 25
  • I'd you don't mind me asking: how does it work for you with FakeGPS? I installed the app (from store), moved the .apk (it's called base.apk) to /system/app, and after the reboot the app works. However, i cant spoof locations while "allow mock locations" is off, and if i turn it on, then spoof using the a pp and then turn "allow mock locations" off, the spoof automatically ends. Mind sharing what I'm doing wrong? – Giladiald Jul 13 '16 at 17:03
  • I am no longer using this app. But you might find the description in the playstore usefull: https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en – lukassteiner Jul 14 '16 at 08:32

3 Answers3

14

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

private int setMockLocationSettings() {
    int value = 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();
    }
    return value;
}

private void restoreMockLocationSettings(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 */
int value = setMockLocationSettings();//toggle ALLOW_MOCK_LOCATION on
try {
    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.

PageNotFound
  • 2,320
  • 2
  • 23
  • 34
  • I didn't try this code in Marshmallow before, but it works for Lollipop.@Marlon – PageNotFound Jul 15 '16 at 09:05
  • 2
    Yeah, it doesn't seem to work in marshmallow, Since google updated the system mock settings to an app picker, I cant get this to work anymore. I have decompiled com.incorporateapps.fakegps because users say that it can do this in marshmallow. The code looks the same above but it's not working for me, I don't know what i'm missing. Can you look at it? @PageNotFound – Marlon Jul 15 '16 at 12:36
  • @PageNotFound: This doesn't work for me. It errors out with 'java.lang.IllegalArgumentException: Provider "gps" unknown' when calling setTestProviderLocation(LocationManager.GPS_PROVIDER. I'm running 4.4.4. – stan Jul 21 '16 at 23:13
  • @PageNotFound just moving the app to system/app is enough ? No need to be signed via system signature ? – blackkara Aug 04 '16 at 12:22
  • @Blackkara Yes, no need to sign with system signature. – PageNotFound Aug 05 '16 at 07:34
  • @PageNotFound I'm implemanting same code you wrote above, and moved the apk to system partition(system/app), but it says 'Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS' What could be wrong with me ? – blackkara Aug 05 '16 at 08:16
  • @Blackkara add `` in `AndroidManifest.xml` – PageNotFound Aug 05 '16 at 09:03
  • @PageNotFound Ohh i was stuck on signing the app via system signature.. Thanks, its working now with this permission. – blackkara Aug 09 '16 at 01:56
2

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

jaaaawn
  • 56
  • 7
2

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.

gonjay
  • 727
  • 2
  • 7
  • 13