繁体   English   中英

使用暂停和继续按钮下载文件?

[英]Download file with pause and resume button?

我已经用Google搜索了很多,但是找不到关于两个暂停和继续按钮的解决方案。
我使用此代码this
但是我不知道如何实现暂停和恢复功能以及如何设置暂停和恢复按钮的onclick事件:

pausebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopdownload(); AND resumedownload(); <------
        }
    });


提前致谢。

停止下载通话

downloadTask.cancel(false);

为了支持恢复下载,您可以使用Java中的Resume http file download中的代码:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
    File file=new File(DESTINATION_PATH);
    if(file.exists()){
         downloaded = (int) file.length();
         connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
    }
}else{
    connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
    bout.write(data, 0, x);
     downloaded += x;
     progressBar.setProgress(downloaded);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM