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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)