Android - 允许在设备所有者应用程序中安装非市场应用程序失败

标签 android device-policy-manager device-owner

我正在尝试以编程方式设置安全设置“未知来源 - 允许安装来自未知来源的应用程序”。

为此,DevicePolicyManager (级别 21)提供了一个函数(“SetSecureSetting”)来设置此设置(仅适用于配置文件或设备所有者)。

在我的 Device-Owner-App(通过 NFC 配置部署)中,我尝试了以下代码:

public void allowNonMarketApps() {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
    try {
      devicePolicyManager.setSecureSetting(deviceAdmin, Settings.Secure.INSTALL_NON_MARKET_APPS, "1");
    } catch (SecurityException e) {
      Log.d(TAG, "securityException caught");
    }
}

尽管我的应用程序是设备所有者,但我总是会收到 SecurityException!

谁能帮我解决这个问题?

提前致谢!

编辑 SecurityException 的输出告诉我设备所有者不能更改此设置:

java.lang.SecurityException: Permission denial: Device owners cannot update install_non_market_apps
W/System.err(27634):    at android.os.Parcel.readException(Parcel.java:1547)
W/System.err(27634):    at android.os.Parcel.readException(Parcel.java:1499)
W/System.err(27634):    at android.app.admin.IDevicePolicyManager$Stub$Proxy.setSecureSetting(IDevicePolicyManager.java:7158)
W/System.err(27634):    at android.app.admin.DevicePolicyManager.setSecureSetting(DevicePolicyManager.java:3753)

我很困惑,因为文档讲述的是不同的东西:

public void setSecureSetting (ComponentName admin, String setting, String value) 

(Added in API level 21)

Called by profile or device owners to update Settings.Secure settings. Validation that the value of the setting is in the correct form for the setting type should be performed by the caller.

The settings that can be updated by a profile or device owner with this method are:

DEFAULT_INPUT_METHOD

INSTALL_NON_MARKET_APPS

SKIP_FIRST_USE_HINTS

最佳答案

显然,API 22 中添加了更改 INSTALL_NON_MARKET_APPS 的能力。

您可以在 2014 年 10 月 21 日完成的代码库中找到更改(API 21 发布后)here :

This allows work profile MDM to enable unknown sources even if the user doesn't have UI for it. Installing an app from an unknown source will still prompt the user with the package installer dialog, so it's not like the MDM can now quietly install apps from non-market sources

API 21 中明确缺少此功能。

关于Android - 允许在设备所有者应用程序中安装非市场应用程序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723664/

相关文章:

android - 使用 DevicePolicyManager .lockNow() 后无法解锁我的手机

android - 设备所有者应用程序是否获得系统应用程序权限?

android - 如何在可能没有托管配置提供程序的应用程序中使用托管配置?

android - 通过 NFC 注册 Lollipop (5.1) 的自助服务终端设备

java - 如何在 Android 工具栏导航图标中设置复选框?

android - 单击 RecyclerView 外部以隐藏 RecyclerView 行中的 ChildView

具有透明布局的 Android Activity

android - 带有 GET_SIGNING_CERTIFICATES 的 PackageManager.getPackageArchiveInfo 返回空签名信息

Android:打开文件夹对话框