一个用于下载的android库。
主要特性如下:
1、使用简单,只需要下载地址即可;
2、可以猜测下载任务名称
3、自动设置下载路径
4、支持断点续传
5、所有监听返回的接口,例如onAdd等,都支持UI操作
权限
使用例子:
DownloadManager downloadManager = new DownloadManager(this);
DownloadTask task = new DownloadTask(this);
task.setUrl("https://github.com/snowdream/android-autoupdate/raw/master/docs/test/android-autoupdater-v0.0.2-release.apk");
downloadManager.add(task, listener); //Add the task
downloadManager.start(task, listener); //Start the task
downloadManager.stop(task, listener); //Stop the task if you exit your APP.
private DownloadListener listener = new DownloadListener() {
/**
* The download task has been added to the sqlite.
*
* operation of UI allowed.
*
* @param downloadTask the download task which has been added to the sqlite.
*/
@Override
public void onAdd(DownloadTask downloadTask) {
super.onAdd(downloadTask);
Log.i("onAdd()");
list.add(downloadTask);
Log.i(""+downloadTask);
adapter.notifyDataSetChanged();
}
/**
* The download task has been delete from the sqlite
*
* operation of UI allowed.
*
* @param downloadTask the download task which has been deleted to the sqlite.
*/
@Override
public void onDelete(DownloadTask downloadTask) {
super.onDelete(downloadTask);
Log.i("onDelete()");
}
/**
* The download task is stop
*
* operation of UI allowed.
*
* @param downloadTask the download task which has been stopped.
*/
@Override
public void onStop(DownloadTask downloadTask) {
super.onStop(downloadTask);
Log.i("onStop()");
}
/**
* Runs on the UI thread before doInBackground(Params...).
*/
@Override
public void onStart() {
super.onStart();
Log.i("onStart()");
}
/**
* Runs on the UI thread after publishProgress(Progress...) is invoked. The
* specified values are the values passed to publishProgress(Progress...).
*
* @param values The values indicating progress.
*/
@Override
public void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
((DownloadTaskAdapter) getListAdapter()).notifyDataSetChanged();
Log.i("onProgressUpdate");
}
/**
* Runs on the UI thread after doInBackground(Params...). The specified
* result is the value returned by doInBackground(Params...). This method
* won't be invoked if the task was cancelled.
*
* @param downloadTask The result of the operation computed by
* doInBackground(Params...).
*/
@Override
public void onSuccess(DownloadTask downloadTask) {
super.onSuccess(downloadTask);
Log.i("onSuccess()");
}
/**
* Applications should preferably override onCancelled(Object). This method
* is invoked by the default implementation of onCancelled(Object). Runs on
* the UI thread after cancel(boolean) is invoked and
* doInBackground(Object[]) has finished.
*/
@Override
public void onCancelled() {
super.onCancelled();
Log.i("onCancelled()");
}
@Override
public void onError(Throwable thr) {
super.onError(thr);
Log.i("onError()");
}
/**
* Runs on the UI thread after doInBackground(Params...) when the task is
* finished or cancelled.
*/
@Override
public void onFinish() {
super.onFinish();
Log.i("onFinish()");
}
};