内容来源自网络资源总结:
有关7.0之后应用间共享文件的详细适配,查看鸿洋大神博客Android 7.0 行为变更 通过FileProvider在应用间共享文件吧
升级包下载到本地之后,安装方法如下:
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N需要在AndroidManifest.xml中添加provider适配
1、res下新建xml文件夹,并创建filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--Environment.getExternalStorageDirectory().getAbsolutePath() + "/apk/"-->
<files-path
name="downloadDir"
path="apk/" />
<external-path
name="downloadDir"
path="apk/" />
</paths>
2、AndroidManifest.xml中添加provider标签
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
3、调用系统安装
public void installApk(String apkFileName) {
try {
String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/apk/";
File apkfile = new File(dir + apkFileName);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", apkfile);
} else {
uri = Uri.fromFile(apkfile);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}