android 打开apk文件(Android安装APK文件)

<provider android:name="androidx.core.content.FileProvider" android:authorities="包名.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ,我来为大家讲解一下关于android 打开apk文件?跟着小编一起来看一看吧!

android 打开apk文件(Android安装APK文件)

android 打开apk文件

在AndroidManifest.xml添加provider

<provider android:name="androidx.core.content.FileProvider" android:authorities="包名.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>

file_paths文件:

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/APK/res/android"> <external-path name="external_files" path="."/> </paths>

安装APK

File completeApkfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), aApkName); if (!completeApkfile.exists()) { return; } completeApkfile.renameTo(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), aApkName.replace(".md", ".apk"))); File apkfile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), aApkName.replace(".md", ".apk")); // 通过Intent安装APK文件 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri apkUri; // Android 7.0 以上不支持 file://协议 需要通过 FileProvider 访问 sd卡 下面的文件,所以 Uri 需要通过 FileProvider 构造,协议为 content:// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 声明需要的临时权限 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // content:// 协议 apkUri = FileProvider.getUriForFile(mContext.getapplicationContext(), "包名.fileProvider", apkfile); } else { // file:// 协议 apkUri = Uri.fromFile(apkfile); } intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); aContext.startActivity(intent);

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页