App升级,下载,安装,Notification适配8.0

作者 : 开心源码 本文共5389个字,预计阅读时间需要14分钟 发布时间: 2022-05-12 共227人阅读

前言

好久没有写文章了,今天就简单做一个APP检测升级的小工具,有点粗糙。支持断点续传,notification通知显示,下载完成自动安装,自己可根据大家的想法增加更多的功能,这里只是为了想我一样的初学者和比较简约的人所提供。

App升级思路

当我们点击检查升级的时候,就会向服务器发起版本检测的请求。一般的解决方式是:服务器返回的App版本与当前手机安装的版本号进行比照。

  1. 假如服务器所返回的版本号大于当前App版本号那么此时手机所安装的App不是最新版。可以提醒使用户更新。
  2. 假如不大于当前版本号,可以提醒使用户为最新版本。

准备工作

(一)权限(应使用联网,存储,允许安装)
 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这里自己可以去看一下Android的权限申请方法

(二)下载网络请求库

我这里用的retrofit2+rxjava2来进行网络请求,在.gradle中增加依赖就可,简单方便。

  implementation 'io.reactivex.rxjava2:rxjava:2.1.9'  implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'   //网络请求框架 retrofit  implementation 'com.squareup.retrofit2:retrofit:2.3.0'  implementation 'com.squareup.retrofit2:converter-gson:2.3.0'  implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
(三)适配7.0的安装apk
  1. 7.0之前
    在7.0之前安装的时候,只要要通过隐式Intent来跳转,并且指定安装的文件Uri就可
Intent intent = new Intent(Intent.ACTION_VIEW);// 因为没有在Activity环境下启动Activity,设置下面的标签intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(Uri.fromFile(new File(apkPath)),                    "application/vnd.android.package-archive");context.startActivity(intent);
  1. 7.0之后
    在Android7.0之后的版本运行上述代码会出现android.os.FileUriExposedException
    “私有目录被限制访问”是指在Android7.0中为了提高私有文件的安全性,面向 Android N 或者更高版本的应使用私有目录将被限制访问。
    而7.0的” StrictMode API 政策” 是指禁止向你的应使用外公开 file:// URI。 假如一项包含文件 file:// URI类型 的 Intent 离开你的应使用,应使用失败,并出现 FileUriExposedException 异常。
    之前代码使用到的Uri.fromFile就是商城一个file://的Uri
    在7.0之后,我们需要用FileProvider来处理
    第一步:在AndroidManifest.xml清单文件中注册provider
        <provider            android:name=".MyFileProvider"            android:authorities="${applicationId}.provider"            android:exported="false"            android:grantUriPermissions="true">            <meta-data               android:name="android.support.FILE_PROVIDER_PATHS"                android:resource="@xml/file_paths" />        </provider>

需要注意一下几点:

  1. exported:必需为false
  2. grantUriPermissions:true,表示授予 URI 临时访问权限。
  3. authorities 组件标识,都以包名开头,避免和其它应使用发生冲突。
    第二步:指定共享文件的目录,需要在res文件夹中新建xml目录,并且创立file_paths
<?xml version="1.0" encoding="utf-8"?><paths>    <external-path        name="files_root"        path="com.cq.dlm.appupdatedemo/" />    <external-path        name="external_storage_root"        path="." /></paths>

第三步:用

       Intent intent = new Intent(Intent.ACTION_VIEW);//判断能否是AndroidN以及更高的版本        String authority = getPackageName() + ".provider";        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            Uri contentUri = FileProvider.getUriForFile(this, authority, file);            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);            intent.setDataAndType(contentUri, "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);
(四)文件下载
apiService.executeDownload("bytes=" + Long.toString(range) + totalLength, url)                .subscribe(new Observer<ResponseBody>() {                    @Override                    public void onSubscribe(Disposable d) {                    }                    @Override                    public void onNext(ResponseBody responseBody) {                        RandomAccessFile randomAccessFile = null;                        InputStream inputStream = null;                        long total = range;                        long responseLength = 0;                        try {                            byte[] buf = new byte[2048];                            int len = 0;                            responseLength = responseBody.contentLength();                            inputStream = responseBody.byteStream();                            String filePath = Constants.APP_ROOT_PATH + Constants.DOWNLOAD_DIR;                            File file = new File(filePath, fileName);                            File dir = new File(filePath);                            if (!dir.exists()) {                                dir.mkdirs();                            }                            randomAccessFile = new RandomAccessFile(file, "rwd");                            if (range == 0) {                                randomAccessFile.setLength(responseLength);                            }                            randomAccessFile.seek(range);                            int progress = 0;                            int lastProgress = 0;                            while ((len = inputStream.read(buf)) != -1) {                                randomAccessFile.write(buf, 0, len);                                total += len;                                lastProgress = progress;                                progress = (int) (total * 100 / randomAccessFile.length());                                if (progress > 0 && progress != lastProgress) {                                    downloadCallback.onProgress(progress);                                }                            }                            downloadCallback.onCompleted();                        } catch (Exception e) {                            Log.d(TAG, e.getMessage());                            downloadCallback.onError(e.getMessage());                            e.printStackTrace();                        } finally {                            try {                                SPDownloadUtil.getInstance().save(url, total);                                if (randomAccessFile != null) {                                    randomAccessFile.close();                                }                                if (inputStream != null) {                                    inputStream.close();                                }                            } catch (Exception e) {                                e.printStackTrace();                            }                        }                    }                    @Override                    public void onError(Throwable e) {                        downloadCallback.onError(e.toString());                    }                    @Override                    public void onComplete() {                    }                });
(五)通知栏显示,Notifications适配到8.0

Android8.0已经出了很久了,notification主要涉及到NotificationChannel(通道),主要是为了方便管理通知栏

  mNotifyManager = (NotificationManager)                getSystemService(Context.NOTIFICATION_SERVICE);        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//适配8.0,自行查看8.0的通知,主要是NotificationChannel            NotificationChannel chan1 = new NotificationChannel(PRIMARY_CHANNEL,                    "Primary Channel", NotificationManager.IMPORTANCE_DEFAULT);            chan1.setLightColor(Color.GREEN);            chan1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);            mNotifyManager.createNotificationChannel(chan1);            mBuilder = new NotificationCompat.Builder(this, PRIMARY_CHANNEL);        } else {            mBuilder = new NotificationCompat.Builder(this, null);        }        mBuilder.setContentText(mDownloadFileName)//notification的少量设置,具体的可以去官网查看                .setContentTitle(this.getString(R.string.app_name))                .setTicker("正在下载")                .setPriority(Notification.PRIORITY_DEFAULT)                .setDefaults(Notification.DEFAULT_VIBRATE)                .setOnlyAlertOnce(true)                .setWhen(System.currentTimeMillis())                .setSmallIcon(R.mipmap.ic_launcher);        mBuilder.setProgress(100, progress, false);//显示下载进度        mNotification = mBuilder.build();        mNotifyManager.notify(downloadId, mNotification);
(六)总结

本文主要使用到retrofit+rxjava 实现文件的断点续传,同时对android7.0(文件共享权限)和8.0(notifications通知栏)的相关特性适配,还是比较基础简单,希望能帮助到刚入坑的童鞋。
效果图:

download.jpg
项目地址 仅供参考,如有不正确的地方请指出改正,觉得有使用的可以多多支持一下。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » App升级,下载,安装,Notification适配8.0

发表回复