Giter Club home page Giter Club logo

android-httpdownloadmanager's Introduction

Android Http Download Manager

An useful and effective http download manager for Android. This download manager is designed according to the idea and implementation of Volley.

Usage

  • If you don't set the destination file path, the download manager will use Environment.DIRECTORY_DOWNLOADS in SDCard as default directory, and it will detect filename automatically from header or url if destinationFilePath not set:
DownloadManager manager = downloadManager =
			new DownloadManager.Builder().context(this)
				.downloader(OkHttpDownloader.create())
				.threadPoolSize(2)
				.logger(logger)
				.build();
String destPath = Environment.getExternalStorageDirectory() + File.separator + "test.apk";
DownloadRequest request = new DownloadRequest.Builder()
				.url("http://something.to.download")
				.retryTime(5)
				.retryInterval(2, TimeUnit.SECONDS)
				.progressInterval(1, TimeUnit.SECONDS)
				.priority(Priority.HIGH)
				.allowedNetworkTypes(DownloadRequest.NETWORK_WIFI)
				.destinationFilePath(destPath)
				.downloadCallback(new DownloadCallback() {
					@Override public void onStart(int downloadId, long totalBytes) {
						
					}

					@Override public void onRetry(int downloadId) {
						
					}

					@Override
					public void onProgress(int downloadId, long bytesWritten, long totalBytes) {
						
					}

					@Override public void onSuccess(int downloadId, String filePath) {
						
					}

					@Override public void onFailure(int downloadId, int statusCode, String errMsg) {
						
					}
				})
				.build();
				
int downloadId = manager.add(request);

It's easy to stop:

	/* stop single */
	manager.cancel(downloadId);
	/* stop all */
	manager.cancelAll();
  • If you don't want to set the filename but want to set the download directory, then you can use destinationDirectory(String directory), but this method will be ignored if destinationFilePath((String filePath) was used.
  • You can also set retry time with method retryTime(int retryTime) if necessary, default retry time is 1. You can set retry interval to decide how long to retry with method retryInterval(long interval, TimeUnit unit).
  • This manager support downloading in different network type with method allowedNetworkTypes(int types), the types can be DownloadRequest.NETWORK_MOBILE and DownloadRequest.NETWORK_WIFI. This method need android.permission.ACCESS_NETWORK_STATE permission.
  • The thread pool size of download manager is 3 by default. If you need a larger pool, then you can try the method threadPoolSize(int poolSize) in DownloadManager#Builder.
  • You need android.permission.WRITE_EXTERNAL_STORAGE permission if you don't use public directory in SDCard as download destination file path. Don't forget to add android.permission.INTERNET permission.
  • This download manager support breakpoint downloading, so you can restart the downloading after pause.
  • If you don't want DownloadDispathcer invoke onProgress(int downloadId, long bytesWritten, long totalBytes) frequently, then you can use progressInterval(long interval, TimeUnit unit).
  • If you want one download request get high priority, then you can use priority(Priority priority).
  • The download manager provides two kinds of Downloader(URLDownloader and OkHttpDownloader), and the it will detect which downloader to use. You can also implement your own Downloader just like what URLDownloader and OkHttpDownloader do.

Download

compile 'com.coolerfall:android-http-download-manager:1.6.1'

Note

If you're using OkHttpDownloader with custom OkHttpClient as Downloader in DownloadManager, then you should not add HttpLoggingInterceptor in your custom OkHttpClient. It may be crashed(OOM) as HttpLoggingInterceptor use okio to reqeust the whole body in memory.

Credits

  • Volley - Google networking library for android.

License

Copyright (C) 2014-2016 Vincent Cheung

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.

android-httpdownloadmanager's People

Contributors

coolerfall avatar tian9246 avatar

Watchers

 avatar  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.