android下载框架FileDownloader的简单使用

首先,依赖:

implementation 'com.liulishuo.filedownloader:library:1.7.4'

因为我没有定制组件,所以我直接在实用的地方初始化:

FileDownloader.setup(ac);   //ac为activity的上下文对象

然后直接使用:

String FileLoad = "cxstatus/";
 FileDownloader.getImpl().create(url)    
            .setPath(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+FileLoad+"status.apk")
            .setForceReDownload(true)
            .setListener(new FileDownloadListener() {
               //等待
   @Override
   protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
      progressDialog.show();
   }
   //下载进度回调
   @Override
   protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
      progressDialog.setProgress((soFarBytes * 100 / totalBytes));
   }
   //完成下载
   @Override
   protected void completed(BaseDownloadTask task) {
      progressDialog.cancel();
                    installAPK();
                }
   //暂停
   @Override
   protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

   }
   //下载出错
   @Override
   protected void error(BaseDownloadTask task, Throwable e) {
      Toast.makeText(ac,"下载出错",Toast.LENGTH_SHORT).show();
      progressDialog.cancel();
   }
   //已存在相同下载
   @Override
   protected void warn(BaseDownloadTask task) {
      progressDialog.cancel();
   }
}).start();

很不错的一个下载框架,性能很好;

更多的使用可以去github上去看文档,有中英文的

github地址:https://github.com/lingochamp/FileDownloader

猜你喜欢

转载自blog.csdn.net/blackei/article/details/80388246