android app版本更新8.0,Android App内版本更新完美适配7.0、8.0

Android应用在版本更新上并不像IOS那么暴力(在后台悄悄咪咪给你升级),很多时候还是需要在App内提示用户下载更新的,这也就意味着我们需要自己下载apk,并跳转安装!而随着Android的版本更新迭代,越来越注重应用安全,以及对危险权限的限制,使得我们以前的下载、安装变得不靠谱了,下面我们就来一一填坑。。。

一、兼容Android7.0

Android 7.0 做了一些权限更改,为了提高私有文件的安全性,面向 Android 7.0 或更高版本的应用私有目录被限制访问。

传递软件包网域外的 file:// URI 可能给接收器留下无法访问的路径。因此,尝试传递 file:// URI 会触发 FileUriExposedException。分享私有文件内容的推荐方法是使用FileProvider。

解决方法如下:

1、定义FileProvider

//允许您授予对文件的临时访问权限

2、指定可用文件

在res目录下新建xml包,然后在xml下新建file_paths.xml文件,文件内容如下:

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添加了新的安全验证,不允许应用内安装未经过验证的应用,所以需要添加下面这个权限。

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的各位工程师新春快乐!万事如意!来年挣大钱哈!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值