使用模拟位置而不在设置中设置它 [英] Use mock location without setting it in settings

查看:35
本文介绍了使用模拟位置而不在设置中设置它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它利用了 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,将其移至/system/app,然后重新启动
而且我还尝试了在清单中使用和不使用 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:无法启动 Activity: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
}

由于这些代码的执行时间很短,其他应用很难检测到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. 将您的应用移动到 /system/app/system/priv-app

我在我的项目中尝试过,效果很好.

I tried it in my project and it works fine.

这篇关于使用模拟位置而不在设置中设置它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆