在Android7.0+手上,版本升级完成,发现手机安装完成,不启动安装完成页面,而是直接关闭了,小编也是一头雾水。琢磨了很久,下面小编把解决办法show出来。
第一步:在资源文件下面新建 xml文件夹,新建file_paths.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="files_root"
path="Android/data/com.ymcd.ivappa/" />
<external-path
name="external_storage_root"
path="." />
</paths>
第二步,在AndroidManifest.xml文件配置里面添加
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ymcd.ivappa.fileProvider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
第三步,在代码里面添加下面核心代码
/**
* 安装文件
*
* @param mContext
* @param file
*/
private void installApk(Context mContext, File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// 判断版本大于等于7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 给目标应用一个临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri data = FileProvider.getUriForFile(mContext, "com.example.administrator.newspolice.fileProvider", file);
intent.setDataAndType(data, "application/vnd.android.package-archive");
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
startActivity(intent);
}