简体   繁体   English

如何以编程方式在已扎根的android上禁用/启用gps?

[英]How to programatically disable/enable gps on rooted android?

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

ON Rooted Device try this just use su for enabling gps on high accuracy mode 在根设备上尝试使用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();

Run these command on background thread :) 在后台线程上运行这些命令:)

further you can refer to this link here 您还可以在这里参考此链接

This code works on ROOTED phones if the app is moved to /system/aps , and they have the following permissions in the manifest : 如果应用程序移至 /system/aps此代码在ROOTED手机上适用并且它们在清单中具有以下权限

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

Code

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) {}
}

You aren't supposed to to protect the privacy of the user. 您不应该保护用户的隐私。 However it is possible by exploiting a bug. 但是,可以通过利用错误来实现。 See this for how: 参见以下内容:

How can I enable or disable the GPS programmatically on Android? 如何在Android上以编程方式启用或禁用GPS?

Note that this may not work on all versions of Android - see 请注意,这可能不适用于所有版本的Android-请参阅
https://android.googlesource.com/platform/packages/apps/Settings/+/4b21f7cd9424eeb83838071a4419912ee5d5e41d https://android.googlesource.com/platform/packages/apps/Settings/+/4b21f7cd9424eeb83838071a4419912ee5d5e41d

where they indicate it has been fixed but i'm not sure which versions have the fix (if any). 他们在哪里表明已修复,但我不确定哪个版本已修复(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM