【问题标题】:Android, Change GPS status when app is device administrator enabledAndroid,当应用程序启用设备管理员时更改 GPS 状态
【发布时间】:2015-09-06 04:09:45
【问题描述】:

当应用被用户设置为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已启用的权限,请勿将问题标记为重复!

【问题讨论】:

标签: java android gps


【解决方案1】:

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

Control Secure Settings

Control Global Settings

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2012-04-07
    • 2012-05-21
    • 2014-10-14
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多