luk*_*ner 6 java gps android location mocking
我正在编写一个应用程序,它利用了android中的位置模拟可能性.
我想要实现的是模拟我的位置而不在开发人员选项中设置"允许模拟位置"标志.
我知道这是有可能的,因为适用于此应用:https: //play.google.com/store/apps/details?id = com.lexa.fakegps&hl = zh-CN
我尝试了什么:
生成一个apk,将其移动到/ system/app,重新启动
我也尝试使用清单中的ACCESS_MOCK_LOCATION权限.
但是这一切都导致了这个异常:
RuntimeException:无法启动Activity:SecurityException:需要ACCESS_MOCK_LOCATION安全设置
Pag*_*und 14
我已经反编译com.lexa.fakegps你提到的问题了,它是这样做的:
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
}
Run Code Online (Sandbox Code Playgroud)
由于这些代码的执行时间非常短,其他应用程序很难检测到ALLOW_MOCK_LOCATION的变化.
您还需要,
1.需要root权限访问您的设备
2.将您的应用移动到/system/app或/system/priv-app
我在我的项目中尝试过,它运行正常.
| 归档时间: |
|
| 查看次数: |
67055 次 |
| 最近记录: |