简体   繁体   English

Android,当应用程序已启用设备管理员时更改GPS状态

[英]Android, Change GPS status when app is device administrator enabled

How to set GPS status on when the app is set as Device administrator by user. 当用户将应用设置为Device administrator时,如何设置GPS状态。

I'm using this method : 我正在使用这种方法:

private void turnGPSOn() {
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    getApplicationContext().sendBroadcast(intent);

    String provider = Settings.Secure.getString(getApplicationContext()
            .getContentResolver(),
            Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (!provider.contains("gps")) { // if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        this.getApplicationContext().sendBroadcast(poke);

    }
}

and getting this error at least on API-21 : 并至少在API-21上得到此错误:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE from pid=27737, uid=10464

please please please care about Device administrator permission that is enabled and don't tag the question as duplicated ! 请请注意启用的Device administrator权限,不要将问题标记为重复

android.location.GPS_ENABLED_CHANGE intent can only be broadcasted by system apps as it is a protected broadcast(means your app should be either signed with systemsignature or it should be a system app). android.location.GPS_ENABLED_CHANGE意图只能由系统应用程序广播,因为它是受保护的广播(意味着您的应用程序应该使用systemsignature签名或应该是系统应用程序)。 Even if your app is selected as Device Admin app by user, it does not mean it is eligible to use system features. 即使您的应用被用户选择为Device Admin应用,也不意味着它有资格使用系统功能。 Device Admin app will get access to features that are exposed by DevicePolicyManager class. Device Admin应用程序将有权访问DevicePolicyManager类公开的功能 Some of the global settings and secure settings can be controlled using DevicePolicyManger class on Lollipop and above. 可以使用Lollipop及更高版本上的DevicePolicyManger类来控制某些全局设置和安全设置。

Control Secure Settings 控制安全设置

Control Global Settings 控制全局设置

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

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