![](/img/trans.png)
[英]XMLResourceParserError while trying to install an apk programmatically
[英]Error trying to install APK programmatically
在此之后,我在下載后安裝 apk 的代碼是:
var downloadIntent: Intent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val apkFile = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "update.apk")
val contentUri = FileProvider.getUriForFile(appContext, "${activity?.packageName}.fileprovider", apkFile)
downloadIntent = Intent(Intent.ACTION_INSTALL_PACKAGE)
downloadIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
downloadIntent.setDataAndType(contentUri, "application/vnd.android.package-archive")
}
appContext.startActivity(downloadIntent);
這是清單:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
...
<provider
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:name="androidx.core.content.FileProvider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
結果是 package 安裝程序靜默停止。
感謝@Selvin,必須像這樣設置標志:
downloadIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.