java – 使用模拟位置而不在设置中设置它
发布时间:2020-12-14 23:22:44 所属栏目:Java 来源:网络整理
导读:我正在编写一个应用程序,它利用了 android中的位置模拟可能性. 我想要实现的是模拟我的位置而不在开发人员选项中设置“允许模拟位置”标志. 我知道这是可能的,因为它适用于这个应用程序: https://play.google.com/store/apps/details?id=com.lexa.fakegpshl
我正在编写一个应用程序,它利用了
android中的位置模拟可能性.
我想要实现的是模拟我的位置而不在开发人员选项中设置“允许模拟位置”标志. 我知道这是可能的,因为它适用于这个应用程序: 我尝试了什么: 生成一个apk,将其移动到/ system / app,重新启动 但这一切都导致了这个例外: 解决方法
我已经反编译了你提到的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(),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的变化. 你也需要, 我在我的项目中尝试过,它运行正常. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |