Android Downloader is a open source multithread and mulitask downloadInfo framework for Android.
Try out the sample application on the Apk file.
You can download a jar from GitHub's releases page.
Or use Gradle:
dependencies {
compile 'cn.woblog.android:downloader:1.0.1'
}
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.
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
For more information on GitHub wiki and Javadocs.
<uses-permission android:name="android.permission.INTERNET" />
<service android:name="cn.woblog.android.downloader.DownloadService">
<intent-filter>
<action android:name="cn.woblog.android.downloader.DOWNLOAD_SERVICE" />
</intent-filter>
</service>
downloadManager = DownloadService.getDownloadManager(context.getApplicationContext());
Simple use as follows
//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);
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;
}
}
}
}
- Android SDK: Android Downloader requires a minimum API level of 10.
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.
Pich - @Pichsofts on GitHub, Email is [email protected].
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.