android 8.0安装apk除了要配置provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="包名.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在res目录下添加 xml目录 创建file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--外部存储路径-->
<external-path
name="files_root"
path="Android/data/包名/" />
<external-path
name="external_storage_root"
path="." />
</paths>
还要添加这个权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
调用方式
Intent i = new Intent();
i.setAction("android.intent.action.VIEW");
i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判读版本是否在7.0以上
Uri apkUri = FileProvider.getUriForFile(MainApplication.appContext, MainApplication.appContext.getPackageName()
+ ".fileprovider", new File(filePath));
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
}
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);MainApplication.appContext.startActivity(i);