Android应用在版本更新上并不像IOS那么暴力(在后台悄悄咪咪给你升级),很多时候还是需要在App内提示用户下载更新的,这也就意味着我们需要自己下载apk,并跳转安装!而随着Android的版本更新迭代,越来越注重应用安全,以及对危险权限的限制,使得我们以前的下载、安装变得不靠谱了,下面我们就来一一填坑。。。
一、兼容Android7.0
Android 7.0 做了一些权限更改,为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问。
- 传递软件包网域外的
file://
URI 可能给接收器留下无法访问的路径。因此,尝试传递file://
URI 会触发FileUriExposedException
。分享私有文件内容的推荐方法是使用FileProvider。
解决方法如下:
1、定义FileProvider
<provider
android:name="com.bai.lib.MyFileProvider"
android:authorities="${applicationId}.provider"//根据您控制的域将属性设置为URI权限
android:exported="false"//FileProvider不需要公开
android:grantUriPermissions="true">//允许您授予对文件的临时访问权限
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
2、指定可用文件
在res目录下新建xml包,然后在xml下新建file_paths.xml文件,文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="my_file"
path="" />
</paths>
</resources>
3、安装apk
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
Uri apkUri = FileProvider.getUriForFile(mActivity, mActivity.getPackageName() + ".provider", apkfile);
//添加这一句表示对目标应用临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri,
"application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.parse("file://" + apkfile.getAbsolutePath()),
"application/vnd.android.package-archive");
}
mActivity.startActivity(intent);
二、兼容Android8.0
1、因为8.0添加了新的安全验证,不允许应用内安装未经过验证的应用,所以需要添加下面这个权限。
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//先获取是否有安装未知来源应用的权限
boolean haveInstallPermission = getPackageManager().canRequestPackageInstalls();
if (!haveInstallPermission) {
new RxPermissions(this).request(
Manifest.permission.REQUEST_INSTALL_PACKAGES)
.subscribe(granted ->
//更新操作
} else {
//更新操作
}
} else {
//更新操作
}
这里我用RxPermissions这个库去申请权限,墙裂推荐!贼好用哦!
2、如果你的下载过程创建了一个NotificationManager通知,那么恭喜你和我一样又成功的掉坑里了,继续填。。。
NotificationManager mNotifyManager;
PendingIntent mPendingIntent;
Builder mBuilder;
mNotifyManager = (NotificationManager) mActivity
.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "channel_001";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "updata_channel";
String Description = "This is updata channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.setShowBadge(false);
mNotifyManager.createNotificationChannel(mChannel);
}
mBuilder = new Builder(mActivity, CHANNEL_ID);
到此,所有该填的坑已填完!农历2018年最后一天上班!明天开开心心回家过年!也祝CSDN的各位工程师新春快乐!万事如意!来年挣大钱哈!!!