Java – uses the simulated location without setting it in the settings

I'm writing an application that takes advantage of the location simulation possibilities in Android

What I want to achieve is to simulate my location without setting the "allow simulation location" flag in the developer options

I know this is possible because it applies to this application: https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en

What I tried:

Generate an APK, move it to / system / APP, restart it, and I also use and do not use access in the list_ MOCK_ Location permission attempted

But all this leads to this exception: runtimeException: unable to start activity: SecurityException: access is required_ MOCK_ Location security settings

Solution

I have decompiled the com.com you mentioned lexa. Fakegps, its function is as follows:

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(),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 codes is very short, it is difficult for other applications to detect allow_ MOCK_ Change in location

You also need, 1 Require root user to access your device 2 Move your application to / system / app or / system / priv app

I've tried it in my project and it works normally

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>