加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:22:44 所属栏目:Java 来源:网络整理
导读:我正在编写一个应用程序,它利用了 android中的位置模拟可能性. 我想要实现的是模拟我的位置而不在开发人员选项中设置“允许模拟位置”标志. 我知道这是可能的,因为它适用于这个应用程序: https://play.google.com/store/apps/details?id=com.lexa.fakegpshl
我正在编写一个应用程序,它利用了 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,它的作用是这样的:
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的变化.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读