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.
这篇关于使用模拟位置而不在设置中设置它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用模拟位置而不在设置中设置它
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
