使用DownloadManager进行更新下载,部分机型下载失败解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Luck_Nie/article/details/83418489

一直以来都是用downloadManager进行apk下载的,然后进行更新安装。昨天客服反馈有部分用户更新失败,经过代码研究和网络搜索,推断可能的原因是部分国产机型在系统修改的国产中,对downloadManager进行了阉割!!!(真快被坑死了)

上方案前的代码,绝大部分机型没问题,vivoX6,Vivo7Plus可能会出现这个问题(用户反馈的机型,但是找不到测试机,所以是可能。。。)

上之前的代码

 /**
     * 下载新版本
     *
     * @param context
     * @param url
     */
    public static void downLoadAPK(Context context, String url, File desFile) {
        try {

            String serviceString = Context.DOWNLOAD_SERVICE;
            final DownloadManager downloadManager = (DownloadManager) context.getSystemService(serviceString);

            Uri uri = Uri.parse(url);
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.allowScanningByMediaScanner();
            request.setVisibleInDownloadsUi(true);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setMimeType("application/vnd.android.package-archive");
            if (desFile.exists()) {
                desFile.delete();
            }

            request.setDestinationInExternalPublicDir("mydownload", "NewVersion.apk");

            AppConfig.DownLoadRefrence = downloadManager.enqueue(request);

        } catch (Exception exception) {
            ToastUtil.showToast(context, "更新失败");
        }

    }

出现异常之后,提示用户更新失败。

然后,用户一直更新失败,反馈回来后,产品要求出解决方案,头疼!!!!!!






经过苦思冥想,终于

解决方案

思路,部分机型问题,既然系统的自带下载更新不能用,就另辟蹊径,让这部分用户去浏览器下载

 /**
     * 下载新版本
     *
     * @param context
     * @param url
     */
    public void downLoadAPK(Context context, String url, File desFile, String errorMsg) {
        try {
        //正常下载
            final DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

            Uri uri = Uri.parse(url);
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.allowScanningByMediaScanner();
            request.setVisibleInDownloadsUi(true);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setMimeType("application/vnd.android.package-archive");

            AppConfig.IS_DOWNLOADING = true;

            if (desFile.exists()) {
                desFile.delete();
            }

            request.setDestinationInExternalPublicDir("mydownload", "NewVersion.apk");

            AppConfig.DownLoadRefrence = downloadManager.enqueue(request);

        } catch (Exception exception) {
            //非正常情况
            if (!TextUtils.isEmpty(errorMsg)) {
            //弹出弹出提示用户,如果更新失败,请联系客服
                CommonDialog commonDialog = new CommonDialog(this);
                commonDialog.setTitle("提示");
                commonDialog.setContent(errorMsg);
                commonDialog.showSingleButton(true);
                commonDialog.setCanceledOnTouchOutside(false);
                commonDialog.show();
            }

			//防止崩溃,再抓一下
            try {//携带下载链接跳转到浏览器
                if (!TextUtils.isEmpty(url) && url.contains("http")) {
                    AppConfig.IS_DOWNLOADING = true;
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(url);
                    intent.setData(content_url);
                    startActivity(intent);
                }

            } catch (Exception e) {
             //异常处理
            }
        }

    }

以上就是产品出现的问题了,最近出现很多莫名其妙的用户反馈,就知道个手机号和手机型号,产品狗也不懂,直接让解决,很崩溃的!!!

猜你喜欢

转载自blog.csdn.net/Luck_Nie/article/details/83418489
今日推荐