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

前端之家收集整理的这篇文章主要介绍了java – 使用模拟位置而不在设置中设置它前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个应用程序,它利用了 android中的位置模拟可能性.

我想要实现的是模拟我的位置而不在开发人员选项中设置“允许模拟位置”标志.

我知道这是可能的,因为它适用于这个应用程序:
https://play.google.com/store/apps/details?id=com.lexa.fakegps&hl=en

我尝试了什么:

生成一个apk,将其移动到/ system / app,重新启动
我还在清单中使用和不使用ACCESS_MOCK_LOCATION权限进行了尝试.

但这一切都导致了这个例外:
RuntimeException:无法启动Activity:SecurityException:需要ACCESS_MOCK_LOCATION安全设置

解决方法

我已经反编译了你提到的com.lexa.fakegps,它的作用是这样的:
  1. private int setMocklocationSettings() {
  2. int value = 1;
  3. try {
  4. value = Settings.Secure.getInt(getContentResolver(),Settings.Secure.ALLOW_MOCK_LOCATION);
  5. Settings.Secure.putInt(getContentResolver(),Settings.Secure.ALLOW_MOCK_LOCATION,1);
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. return value;
  10. }
  11.  
  12. private void restoreMocklocationSettings(int restore_value) {
  13. try {
  14. Settings.Secure.putInt(getContentResolver(),restore_value);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19.  
  20. /* every time you mock location,you should use these code */
  21. int value = setMocklocationSettings();//toggle ALLOW_MOCK_LOCATION on
  22. try {
  23. mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER,fake_location);
  24. } catch (SecurityException e) {
  25. e.printStackTrace();
  26. } finally {
  27. restoreMocklocationSettings(value);//toggle ALLOW_MOCK_LOCATION off
  28. }

由于这些代码的执行时间非常短,其他应用程序很难检测到ALLOW_MOCK_LOCATION的变化.

你也需要,
1.要求root用户访问您的设备
2.将您的应用程序移动到/ system / app或/ system / priv-app

我在我的项目中尝试过,它运行正常.

猜你在找的Java相关文章