鸿蒙上安装按钮实现下载、暂停、取消、显示等操作

描述

今天给大家分享在鸿蒙上一个按钮实现下载、暂停、取消、显示下载进度操作。

先看效果,如下图:

开始

 

暂停

下载中

 

下载完成

 

取消下载

开始写代码

①PRDownloader 的配置

  下载功能使用的是 PRDownloader:

https://gitee.com/chinasoft_ohos/PRDownloader

 

首先,在 moudle 级别下的 build.gradle 文件中添加依赖:

implementation 'com.gitee.chinasoft_ohos1.0.0'
   

 

在 config.json 中添加互联网许可:

 

  "deviceConfig": {
    "default": {
      "directLaunch": false,
      "network": {
        "cleartextTraffic": true
      }
    }
  }

 

 

"reqPermissions": [
      {
        "name": "ohos.permission.INTERNET"
      }
    ]

 

在 AbilitySlice 的 onStart() 方法中初始化它。

 

PRDownloader.initialize(getApplicationContext());
   

 

②创建一个 ProgressBar 进度条

布局文件:

 


   

 

ProgressBar 的点击事件:

 

progressBar.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (downloadId == 0){
                    new ToastDialog(getContext()).setText("开始下载").show();
                    downloadId = PRDownloader.download(url,path,name)
                            .build()
                            // 恢复下载
                            .setOnStartOrResumeListener(new OnStartOrResumeListener() {
                                @Override
                                public void onStartOrResume() {
                                    progressBar.setProgressHintText("正在下载");
                                }
                            })
                            //暂停下载
                            .setOnPauseListener(new OnPauseListener() {
                                @Override
                                public void onPause() {
                                    progressBar.setProgressHintText("恢复下载");
                                }
                            })
                            // 取消下载
                            .setOnCancelListener(new OnCancelListener() {
                                @Override
                                public void onCancel() {
                                    progressBar.setProgressHintText("已取消下载");
                                }
                            })
                            // 下载进程  currentBytes当前下载字节,totalBytes文件总字节。
                            .setOnProgressListener(new OnProgressListener() {
                                @Override
                                public void onProgress(Progress progress) {
                                    long currentBytes = progress.currentBytes;
                                    long totalBytes = progress.totalBytes;
                                    progressBar.setMaxValue((int)totalBytes);
                                    progressBar.setProgressValue((int)currentBytes);
                                    progressBar.setProgressHintText(new SizeUtil().getPrintSize(currentBytes) + "/" + new SizeUtil().getPrintSize(totalBytes));
                                }
                            })
                            .start(new OnDownloadListener() {
                                @Override
                                public void onDownloadComplete() {
                                    progressBar.setProgressHintText("下载完成");
                                    status = PRDownloader.getStatus(downloadId);
                                    text.setText(status.name());
                                }

                                @Override
                                public void onError(Error error) {
                                    progressBar.setProgressHintText("下载出错");
                                    downloadId = 0;
                                    text.setText(error.getServerErrorMessage());
                                }
                            });
                    status = PRDownloader.getStatus(downloadId);
                    text.setText(status.name());
                    //QUEUED RUNNING 下载中
                }else if (status.name().equals("QUEUED") || status.name().equals("RUNNING")){
                    PRDownloader.pause(downloadId);
                    new ToastDialog(getContext()).setText("暂停下载").show();
                    status = PRDownloader.getStatus(downloadId);
                    text.setText(status.name());
                    // PAUSED  暂停
                }else if(status.name().equals("PAUSED")){
                    PRDownloader.resume(downloadId);
                    new ToastDialog(getContext()).setText("恢复下载").show();
                    status = PRDownloader.getStatus(downloadId);
                    text.setText(status.name());
                    // 已完成下载
                }else if(progressBar.getProgressHintText().equals("下载完成")){
                    text.setText("已下载完成,存储路径:
" + path);
                }
            }
        });
   

 

取消下载的操作:(因为需要在一个按钮上实现所以操作,所以取消使用长按来实现)

 

progressBar.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                new ToastDialog(getContext()).setText("取消下载").show();
                PRDownloader.cancel(downloadId);
                downloadId = 0;
                progressBar.setProgressValue(0);
                progressBar.setProgressHintText("开始下载");
                status = PRDownloader.getStatus(downloadId);
                text.setText(status.name());
            }
        });
 

原文标题:鸿蒙手机上一个按钮实现下载操作!

文章出处:【微信公众号:HarmonyOS技术社区】欢迎添加关注!文章转载请注明出处。

  审核编辑:彭菁
 

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分