美文网首页Android技术知识Android开发经验谈Android知识
App更新,下载,安装,Notification适配8.0

App更新,下载,安装,Notification适配8.0

作者: 副业小侦探 | 来源:发表于2018-08-29 16:41 被阅读44次

    前言

    好久没有写文章了,今天就简单做一个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
    项目地址 仅供参考,如有不正确的地方请指出改正,觉得有用的可以多多支持一下。

    相关文章

      网友评论

      本文标题:App更新,下载,安装,Notification适配8.0

      本文链接:https://www.haomeiwen.com/subject/sahkwftx.html