dengjiaping / qixing

2017.12.07 七星时代第一次上传

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AndroidDownloader

Build Status Codacy Badge Issue Count Maven-central Version Release License

Report an issue

Android Downloader is a open source multithread and mulitask downloadInfo framework for Android.

Try out the sample application on the Apk file.

AndroidDownloader Sample Screenshots AndroidDownloader Sample Screenshots

AndroidDownloader Sample Screenshots AndroidDownloader Sample Screenshots

Download

You can download a jar from GitHub's releases page.

Or use Gradle:

dependencies {
  compile 'cn.woblog.android:downloader:1.0.0'
}

Or Maven:

<dependency>
  <groupId>cn.woblog.android</groupId>
  <artifactId>downloader</artifactId>
  <version>1.0.0</version>
</dependency>

For info on using the bleeding edge, see the Snapshots wiki page.

ProGuard

If your project uses ProGuard, you need to add the following configuration to your project proguard-rules.pro file

-keep public class * implements cn.woblog.android.downloader.db.DownloadDBController

How do I use Android Downloader?

For more information on GitHub wiki and Javadocs.

0.Add network network permissions()

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

1.Configuration download service

<service android:name="cn.woblog.android.downloader.DownloadService">
  <intent-filter>
    <action android:name="cn.woblog.android.downloader.DOWNLOAD_SERVICE" />
  </intent-filter>
</service>

2.Create a DownloadManager instance

downloadManager = DownloadService.getDownloadManager(context.getApplicationContext());

Simple use as follows

3.Download a file

//create download info set download uri and save path.
final DownloadInfo downloadInfo = new DownloadInfo.Builder().setUrl("http://example.com/a.apk")
    .setPath("/sdcard/a.apk")
    .build();

//set download callback.
downloadInfo.setDownloadListener(new DownloadListener() {

  @Override
  public void onStart() {
    tv_download_info.setText("Prepare downloading");
  }

  @Override
  public void onWaited() {
    tv_download_info.setText("Waiting");
    bt_download_button.setText("Pause");
  }

  @Override
  public void onPaused() {
    bt_download_button.setText("Continue");
    tv_download_info.setText("Paused");
  }

  @Override
  public void onDownloading(long progress, long size) {
    tv_download_info
        .setText(FileUtil.formatFileSize(progress) + "/" + FileUtil
            .formatFileSize(size));
    bt_download_button.setText("Pause");
  }

  @Override
  public void onRemoved() {
    bt_download_button.setText("Download");
    tv_download_info.setText("");
    downloadInfo = null;
  }

  @Override
  public void onDownloadSuccess() {
    bt_download_button.setText("Delete");
    tv_download_info.setText("Download success");
  }

  @Override
  public void onDownloadFailed(DownloadException e) {
    e.printStackTrace();
    tv_download_info.setText("Download fail:" + e.getMessage());
  }
});

//submit download info to download manager.
downloadManager.download(downloadInfo);

4.Use in list

The default is to use the RecyclerView.

class ViewHolder extends RecyclerView.ViewHolder {

  private final ImageView iv_icon;
  private final TextView tv_size;
  private final TextView tv_status;
  private final ProgressBar pb;
  private final TextView tv_name;
  private final Button bt_action;
  private DownloadInfo downloadInfo;

  public ViewHolder(View view) {
    super(view);

    iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
    tv_size = (TextView) view.findViewById(R.id.tv_size);
    tv_status = (TextView) view.findViewById(R.id.tv_status);
    pb = (ProgressBar) view.findViewById(R.id.pb);
    tv_name = (TextView) view.findViewById(R.id.tv_name);
    bt_action = (Button) view.findViewById(R.id.bt_action);
  }

  @SuppressWarnings("unchecked")
  public void bindData(final MyDownloadInfo data, int position, final Context context) {
    Glide.with(context).load(data.getIcon()).into(iv_icon);
    tv_name.setText(data.getName());

    // Get download task status
    downloadInfo = downloadManager.getDownloadById(data.getUrl().hashCode());

    // Set a download listener
    if (downloadInfo != null) {
      downloadInfo
          .setDownloadListener(new MyDownloadListener(new SoftReference(ViewHolder.this)) {
            //  Call interval about one second
            @Override
            public void onRefresh() {
              if (getUserTag() != null && getUserTag().get() != null) {
                ViewHolder viewHolder = (ViewHolder) getUserTag().get();
                viewHolder.refresh();
              }
            }
          });

    }

    refresh();

//      Download button
    bt_action.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (downloadInfo != null) {

          switch (downloadInfo.getStatus()) {
            case DownloadInfo.STATUS_NONE:
            case DownloadInfo.STATUS_PAUSED:
            case DownloadInfo.STATUS_ERROR:

              //resume downloadInfo
              downloadManager.resume(downloadInfo);
              break;

            case DownloadInfo.STATUS_DOWNLOADING:
            case DownloadInfo.STATUS_PREPARE_DOWNLOAD:
            case STATUS_WAIT:
              //pause downloadInfo
              downloadManager.pause(downloadInfo);
              break;
            case DownloadInfo.STATUS_COMPLETED:
              downloadManager.remove(downloadInfo);
              break;
          }
        } else {
//            Create new download task
          File d = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "d");
          if (!d.exists()) {
            d.mkdirs();
          }
          String path = d.getAbsolutePath().concat("/").concat(data.getName());
          downloadInfo = new Builder().setUrl(data.getUrl())
              .setPath(path)
              .build();
          downloadInfo
              .setDownloadListener(new MyDownloadListener(new SoftReference(ViewHolder.this)) {

                @Override
                public void onRefresh() {
                  if (getUserTag() != null && getUserTag().get() != null) {
                    ViewHolder viewHolder = (ViewHolder) getUserTag().get();
                    viewHolder.refresh();
                  }
                }
              });
          downloadManager.download(downloadInfo);
        }
      }
    });

  }

  private void refresh() {
    if (downloadInfo == null) {
      tv_size.setText("");
      pb.setProgress(0);
      bt_action.setText("Download");
      tv_status.setText("not downloadInfo");
    } else {
      switch (downloadInfo.getStatus()) {
        case DownloadInfo.STATUS_NONE:
          bt_action.setText("Download");
          tv_status.setText("not downloadInfo");
          break;
        case DownloadInfo.STATUS_PAUSED:
        case DownloadInfo.STATUS_ERROR:
          bt_action.setText("Continue");
          tv_status.setText("paused");
          try {
            pb.setProgress((int) (downloadInfo.getProgress() * 100.0 / downloadInfo.getSize()));
          } catch (Exception e) {
            e.printStackTrace();
          }
          tv_size.setText(FileUtil.formatFileSize(downloadInfo.getProgress()) + "/" + FileUtil
              .formatFileSize(downloadInfo.getSize()));
          break;

        case DownloadInfo.STATUS_DOWNLOADING:
        case DownloadInfo.STATUS_PREPARE_DOWNLOAD:
          bt_action.setText("Pause");
          try {
            pb.setProgress((int) (downloadInfo.getProgress() * 100.0 / downloadInfo.getSize()));
          } catch (Exception e) {
            e.printStackTrace();
          }
          tv_size.setText(FileUtil.formatFileSize(downloadInfo.getProgress()) + "/" + FileUtil
              .formatFileSize(downloadInfo.getSize()));
          tv_status.setText("downloading");
          break;
        case STATUS_COMPLETED:
          bt_action.setText("Delete");
          try {
            pb.setProgress((int) (downloadInfo.getProgress() * 100.0 / downloadInfo.getSize()));
          } catch (Exception e) {
            e.printStackTrace();
          }
          tv_size.setText(FileUtil.formatFileSize(downloadInfo.getProgress()) + "/" + FileUtil
              .formatFileSize(downloadInfo.getSize()));
          tv_status.setText("success");
          break;
        case STATUS_REMOVED:
          tv_size.setText("");
          pb.setProgress(0);
          bt_action.setText("Download");
          tv_status.setText("not downloadInfo");
        case STATUS_WAIT:
          tv_size.setText("");
          pb.setProgress(0);
          bt_action.setText("Pause");
          tv_status.setText("Waiting");
          break;
      }

    }
  }
}

Compatibility

  • Android SDK: Android Downloader requires a minimum API level of 10.

Build

Samples

Follow the steps in the Build section to setup the project and then:

./gradlew :samples:run

You may also find precompiled APKs on the releases page.

Author

Pich - @Pichsofts on GitHub, Email is Pichsofts@gmail.com.

License

Copyright 2016 Renpingqing, All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

2017.12.07 七星时代第一次上传


Languages

Language:Java 99.6%Language:JavaScript 0.2%Language:HTML 0.1%Language:Shell 0.1%