Giter Site home page Giter Site logo

downloader's Introduction

Downloader 文件下载器

DownloaderLibrary

Download GitHub forks GitHub stars

  • HttpURLConnection下载文件依赖库,实现多任务单线程断点下载
  • 临时文件的下载长度作为断点标记

权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

导入Android Studio

添加jCenter远程依赖到module里的build.gradle:

dependencies {
    compile 'com.excellence:downloader:1.1.0'
    // 或者直接使用最新版本
    // compile 'com.excellence:downloader:+'
  }

或者直接添加本地Library依赖

compile project(':DownloaderLibrary')

使用示例

  • onCreate方法中初始化

    // 默认任务数2,单任务单线程下载
    Downloader.init(Context context)
    // 设置最大下载的任务数,单任务下载的线程数
    Downloader.init(Context context, int parallelTaskCount, int threadCount)
  • finish结束所有任务

    // 暂停所有下载任务,使用文件长度保存断点
    Downloader.destroy();
  • 监听两种方式

    推荐使用注解方式监听

    • 注解监听

      // 注册
      Downloader.register(this);
      
      // 解绑
      Downloader.unregister(this);
      
      // 监听
      @Download.onPreExecute
      public void onPre(DownloadTask task)
      {
          /**
           * 注解不添加URL,则获取全部任务的下载监听;
           * 加了URL,则过滤出对应的任务的下载监听
           * 如:@Download.onPreExecute({QQ_URL, ANGRYBIRDS_URL})
           */
      }
      
      @Download.onProgressChange
      public void onProgressChange(DownloadTask task)
      {
          /**
           * @see #onPre(DownloadTask)
           */
      }
      
      @Download.onProgressSpeedChange
      public void onProgressSpeedChange(DownloadTask task)
      {
          /**
           * @see #onPre(DownloadTask)
           */
      }
      
      @Download.onCancel
      public void onCancel(DownloadTask task)
      {
          /**
           * @see #onPre(DownloadTask)
           */
      }
      
      @Download.onError
      public void onError(DownloadTask task)
      {
          /**
           * @see #onPre(DownloadTask)
           */
      }
      
      @Download.onSuccess
      public void onSuccess(DownloadTask task)
      {
          /**
           * @see #onPre(DownloadTask)
           */
      }
    • 添加下载任务,并开始下载

      // 文件路径,下载链接,监听接口可以使用IListener接口,也可以使用Listener监听部分回调
      Downloader.addTask(File file, String DownloadURL, new IListener()
      {
      
          @Override
          public void onPreExecute(long fileSize)
          {
      
          }
      
          @Override
          public void onProgressChange(long fileSize, long downloadedSize)
          {
      
          }
      
          @Override
          public void onProgressChange(long fileSize, long downloadedSize, long speed)
          {
      
          }
      
          @Override
          public void onCancel()
          {
      
          }
      
          @Override
          public void onError(DownloadError error)
          {
      
          }
      
          @Override
          public void onSuccess()
          {
      
          }
      
      }));
  • 暂停下载任务

    DownloadTask.pause();
  • 恢复下载任务

    DownloadTask.resume();
  • 删除下载任务

    DownloadTask.discard();

修改日志

版本 描述
1.1.0 注解监听任务 2017-9-13
1.0.0 多任务单线程下载,临时下载文件长度保存断点记录 2017-8-11

downloader's People

Contributors

veizhang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.