为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Android上以编程方式启用或禁用GPS?

如何在Android上以编程方式启用或禁用GPS?

慕妹3242003 2019-06-03 17:09:43
如何在Android上以编程方式启用或禁用GPS?我知道在Android上用程序打开/关闭GPS的问题有 一直. 讨论 许多 时代,答案总是一样的:“出于安全/隐私的原因,您不能转发到位置首选项屏幕,让用户启用/禁用它。”我知道,不过我最近买了塔斯克在市场上,以及你可以用它完成的许多其他事情中,你可以在进入预先确定的应用程序时设置自动启用gps的规则,并在退出时禁用它(请参阅这里关于如何做到这一点的教程,它只是起作用了!)这个应用程序不能用固件签名键签名,因为它可以在很多Android版本和不同的设备上工作,你甚至不需要扎根。我想在我的应用程序中这样做。当然,我不想破坏用户的隐私,所以我首先问用户是否想用典型的“记住我的决定”复选框自动打开它,如果他回答是,启用它。有没有人知道塔斯克是如何做到这一点的?
查看完整描述

4 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

此代码适用于电话如果应用程序被移动到 /system/aps并且它们在清单中具有以下权限:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/><uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

电码

private void turnGpsOn (Context context) {
    beforeEnable = Settings.Secure.getString (context.getContentResolver(),
                                              Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    String newSet = String.format ("%s,%s",
                                   beforeEnable,
                                   LocationManager.GPS_PROVIDER);
    try {
        Settings.Secure.putString (context.getContentResolver(),
                                   Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                                   newSet); 
    } catch(Exception e) {}}private void turnGpsOff (Context context) {
    if (null == beforeEnable) {
        String str = Settings.Secure.getString (context.getContentResolver(),
                                                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (null == str) {
            str = "";
        } else {                
            String[] list = str.split (",");
            str = "";
            int j = 0;
            for (int i = 0; i < list.length; i++) {
                if (!list[i].equals (LocationManager.GPS_PROVIDER)) {
                    if (j > 0) {
                        str += ",";
                    }
                    str += list[i];
                    j++;
                }
            }
            beforeEnable = str;
        }
    }
    try {
        Settings.Secure.putString (context.getContentResolver(),
                                   Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                                   beforeEnable);
    } catch(Exception e) {}}


查看完整回答
反对 回复 2019-06-03
  • 4 回答
  • 0 关注
  • 1085 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信