android 7.0 apk版本更新,下载之后自动安装
这次是记录项目检查更新,下载新版apk之后,自动安装,7.0之后和之前的自动安装
Android 6.0之前的
使用Intent 进行自动安装
//apkFile是下载好的apk文件
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(apkFile);
intent.setDataAndType(data, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
Android 7.0之后的
上面的代码在7.0的系统上会报下面的错误:
Caused by: android.os.FileUriExposedException
这是因为android7.0系统提高了私有文件的安全性,访问私有文件目录被限制,此设置可防止私有文件的元数据泄漏,如它们的大小或存在性。
传递软件包网域外的 file:// URI 可能给接收器留下无法访问的路径。因此,尝试传递 file:// URI 会触发 FileUriExposedException。分享私有文件内容的推荐方法是使用 FileProvider。
- 1.定义一个FileProvider
<mainfest>
...
<application>
...
<!--用于7.0以上系统自动安装apk-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider"
/>
</provider>
...
</application>
...
</mainfest>
- 2.添加可用权限的文件目录
在res文件夹下创建xml文件夹,然后创建file_provider文件
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--升级-->
<external-cache-path
name="mn_update_external_cache"
path="" />
<cache-path
name="mn_update_cache"
path="" />
</paths>
注:此处的filename(file_provider)与在manifest中provider的
android:resource="@xml/file_provider"
相对应。
- 3.代码中调用,调起安装
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data= FileProvider.getUriForFile(mContext, "com.laihui.pinche.fileProvider", apkFile);
// 给目标应用一个临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(data, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
所以,为了适配,需要在调起apk进行安装时要增加判断,以下是完整代码,适配7.0之前和7.0之后
private void installAPk(File apkFile) {
Intent intent = new Intent(Intent.ACTION_VIEW);
//如果没有设置SDCard写权限,或者没有sdcard,apk文件保存在内存中,需要授予权限才能安装
Uri data;
// 判断版本大于等于7.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// "net.csdn.blog.ruancoder.fileprovider"即是在清单文件中配置的authorities
data = FileProvider.getUriForFile(mContext, "com.laihui.pinche.fileProvider", apkFile);
// 给目标应用一个临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
data = Uri.fromFile(apkFile);
}
intent.setDataAndType(data, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
此处记录在使用中遇到的问题或异常:
这个异常导致项目编译不过,解决之后根据英文猜测 是因为项目中有其他地方(或者第三方)也使用了provider导致
解决
1.双击上图的最后一行,常看出现异常的信息
2.或者点击下图位置
英语水平有限,大概猜测:
这个是google的建议
Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml:170:13-174:19 to override.
大致意思是在manifest的170行增加代码 tools:replace=”android:resource”
依次解决出现的类似问题