【问题标题】:Unable to install apk on android 7 programmatically in work profile mode (Unknown sources)无法在工作配置文件模式下以编程方式在 android 7 上安装 apk(未知来源)
【发布时间】:2020-05-06 06:51:27
【问题描述】:

我正在尝试以“Work Profile”模式 (BYOD) 以编程方式安装 apk。但是有一个我无法解决的问题:无法在 android 版本 7.0(24) 和 7.1(25) 中安装 apk。弹出对话框显示此消息:

您的管理员不允许安装从 来源不明

瞧,我是怎么做的:

val uri = FileProvider.getUriForFile(context,authority,File(filePath))   
val openFileIntent = Intent(Intent.ACTION_VIEW)
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
openFileIntent.data = uri
context.startActivity(openFileIntent)

在清单中:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true"
    tools:replace="android:authorities">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths"
        tools:replace="android:resource" />
</provider>

还有file_provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

【问题讨论】:

  • 您是否尝试在您的设备设置中允许安装来自未知来源的应用程序?
  • @lenik 是的,但它不起作用

标签: android kotlin android-work-profile


【解决方案1】:

这就是我找到解决方案的方法:

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
        devicePolicyManager.setSecureSetting(
            componentName,
            Settings.Secure.INSTALL_NON_MARKET_APPS,
            "1"
        )
    } else {
        devicePolicyManager.addUserRestriction(componentName,
UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 2011-09-15
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多