首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rooted上以编程方式禁用/启用gps?

如何在rooted上以编程方式禁用/启用gps?
EN

Stack Overflow用户
提问于 2011-05-22 08:16:28
回答 3查看 6.1K关注 0票数 2

启动并请求超级用户权限后,我需要做什么才能在我的应用程序中启用/禁用gps?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-22 08:40:11

你不应该保护用户的隐私。然而,通过利用一个bug是可能的。查看以下内容以了解如何:

How can I enable or disable the GPS programmatically on Android?

请注意,这可能并不适用于所有版本的Android -请参阅

https://android.googlesource.com/platform/packages/apps/Settings/+/4b21f7cd9424eeb83838071a4419912ee5d5e41d

他们指出它已经修复,但我不确定哪个版本有修复(如果有的话)。

票数 1
EN

Stack Overflow用户

发布于 2016-03-16 18:09:27

在根设备上尝试此选项,只需使用su在高精度模式下启用gps

代码语言:javascript
复制
 Process proc=Runtime.getRuntime().exec(new String[]{"su",
"pm grant com.your_app_packagename android.permission.WRITE_SECURE_SETTINGS",
"settings put secure location_providers_allowed gps,network,wifi"});
 proc.waitFor();

在后台线程上运行以下命令:)

此外,您还可以参考此链接here

票数 2
EN

Stack Overflow用户

发布于 2017-05-16 12:33:42

此代码适用于根目录下的 phones ,前提是将应用程序移动到 /system/aps,并且他们在清单中具有以下权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

代码

代码语言:javascript
复制
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) {}
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6085254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档