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

时间:2011-05-22 00:16:28

标签: android gps location root su

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

3 个答案:

答案 0 :(得分:2)

ON Rooted Device尝试使用su来在高精度模式下启用gps

 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

答案 1 :(得分:1)

您不应该保护用户的隐私。但是通过利用bug可以实现。请参阅以下内容:

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

请注意,这可能不适用于所有Android版本 - 请参阅
https://android.googlesource.com/platform/packages/apps/Settings/+/4b21f7cd9424eeb83838071a4419912ee5d5e41d

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

答案 2 :(得分:1)

此代码适用于 ROOTED 手机,如果应用已移至 /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) {}
}