微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,它的作用是这样的:
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

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

原文地址:https://www.jb51.cc/java/127703.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐