使用模拟位置而不在设置中设置它

Use mock location without setting it in settings(使用模拟位置而不在设置中设置它)
本文介绍了使用模拟位置而不在设置中设置它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在编写一个应用程序,它利用了 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)