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文件,文件内容如下:

<?xml version="1.0" encoding="utf-8"?>                

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

更多相关文章

  1. android sdk 兼容低版本的处理方法
  2. Android下文件资源管理器雏形实现
  3. Android SELinux开发入门指南之正确姿势解决访问data目录权限问
  4. 阿里Android开发规范:文件与数据库
  5. Cocos2dx播放mp4文件(IOS和Android)
  6. Android——文件存储之外部存储(含源码下载)

随机推荐

  1. Android原生开发:两种提示框
  2. Android按钮实现点击事件的四种方式
  3. Android中浮动按钮
  4. Android(安卓)Studio使用.so库的方式(科大
  5. android通过google API获取天气信息
  6. android之helloworld详解
  7. Android学习之RatingBar评分组件
  8. android中退出整个app应用程序
  9. android booting 启动过程
  10. Android全屏(包含3种隐藏顶部状态栏及标题