我试图以"工作剖面“模式(比亚迪)以编程方式安装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>发布于 2020-05-06 09:13:04
我就是这样找到解决方案的:
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)
}https://stackoverflow.com/questions/61629049
复制相似问题