使用模拟位置而不设置设置它而不、位置

2023-09-06 10:19:44 作者:凉兮

我在写一个应用程序,它利用了位置,在Android的嘲讽可能性。

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.

我知道这是可能的,因为与此应用程序的工作原理: https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en

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

我的尝试:

生成一个APK,将其移动到/系统/应用程序,重新开机 我也尝试过有和没有在清单中的ACCESS_MOCK_LOCATION权限。

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.

但是这一切都导致此异常: RuntimeException的:无法启动的活动:SecurityException异常:需要ACCESS_MOCK_LOCATION安全设置

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

推荐答案

我已经反编译 com.lexa.fakegps 你在问题​​中提到的,它所这样做:

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
}

由于这些code的执行时间很短,其他应用程序几乎看不到ALLOW_MOCK_LOCATION的变化。

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

此外,你需要, 1.需要root权限到您的设备 2.将您的应用程序,以 /系统/应用程序 /系统/私法-应用

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.