Reprinted from
;http://blog.csdn.net/LosingCarryJie/article/details/77688064
Our App may provide users with scenarios to download files or pictures, and then you may consider the following terms
Multi task download
Multi thread Download
Breakpoint resume
High concurrence
Yes, if you download the library by hand, you need to consider these four terms. Next, let's learn about the FileDownloader library. The author of the library has encapsulated these four points very well. How can you miss the open source library of 5000 Star?
The old rule is that you must put other people's github address when quoting other people's Libraries
FileDownloader Github address
1 reference import
compile 'com.liulishuo.filedownloader:library:1.6.4'//See github for the latest version
- 1
2 Global initialization
public class APP extends Application {
@Override
public void onCreate() {
super.onCreate();
FileDownloader.setup(this);//Note that the init method is no longer recommended by the author
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3 specific call
Note that the path here is the file name, not the folder name
Note that the path here is the file name, not the folder name
Note that the path here is the file name, not the folder name
FileDownloader.getImpl().create(url).setWifiRequired(true).setPath(path).setListener(new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
int percent=(int) ((double) soFarBytes / (double) totalBytes * 100);
textView.setText("("+percent+"%"+")");
}
@Override
protected void blockComplete(BaseDownloadTask task) {
}
@Override
protected void completed(BaseDownloadTask task) {
Toast.makeText(MainActivity.this,"Download complete!",Toast.LENGTH_SHORT).show();
textView.setText("("+"100%"+")");
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
}
@Override
protected void warn(BaseDownloadTask task) {
continueDownLoad(task);//If the same task exists, continue downloading
}
}).start();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
private void continueDownLoad(BaseDownloadTask task) {
while (task.getSmallFileSoFarBytes()!=task.getSmallFileTotalBytes()){
int percent=(int) ((double) task.getSmallFileSoFarBytes() / (double) task.getSmallFileTotalBytes() * 100);
textView.setText("("+percent+"%"+")");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
Yes, it's so easy to use! This library is far more powerful than my demo. I just showed its basic usage. If you have your own special needs, please go to github and read the source code carefully~