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

时间:2015-06-20 05:27:36

标签: java android gps

当用户将应用设置为Device administrator时,如何设置GPS状态。

我正在使用这种方法:

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);

    }
}

至少在API-21上收到此错误:

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

请关注启用的 Device administrator 权限,不要将问题标记为重复

1 个答案:

答案 0 :(得分:2)

android.location.GPS_ENABLED_CHANGE 意图只能由系统应用广播,因为它是受保护的广播(意味着您的应用应该使用systemsignature签名,或者它应该是系统应用)。即使您的应用被用户选为设备管理应用,也不意味着它有资格使用系统功能。 Device Admin应用程序可以访问 DevicePolicyManager 类公开的功能。可以使用Lollipop及更高版本上的 DevicePolicyManger 类来控制某些全局设置和安全设置。

Control Secure Settings

Control Global Settings

相关问题