我正在尝试以编程方式设置安全设置“未知来源 - 允许安装来自未知来源的应用程序”。
为此,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/