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

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

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...