Multiple file download manager in listview +progress/pause/resume android

送分小仙女□ 提交于 2019-12-30 04:39:26

问题


i need to make a dynamic download manager for my app in this form :

  1. adding new link to current list
  2. can pause and resume download
  3. deleting complete downloads from custom list

    like this

at first i use this site code for thread downloading.

then , i make a custom list view that Every time user click on the Download button,that download link Will be added.

But i have two problem :

  1. after adding new link to list , all of list will be New!
  2. too, previous unfinished download(s) will be new , as list will be new!

Now , The question is: how i can make a dynamic download manager for my app that , can adding new link to list with pause/resume ability and remove downloaded item from custom list?


Edit - adding Custom-Listview

my custom list-view in this link : https://github.com/saeid-pm/Custom-Listview


回答1:


finally after about 1years(:D) this is one of best solutions :

using this library by add to project with library ,

or with Android Studio dependencies :

 dependencies {
    compile 'com.mani:ThinDownloadManager:1.3.0'
 }

it's one of best and fast (any) file Download library , Too is so simple to use and customize.

for example in my question(in 1 year ago) that i wanted to have Multiple-File-Download , at ease can specify thread pool size by :

ThinDownloadManager downloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE); 

//DOWNLOAD_THREAD_POOL_SIZE = number of threads.

goodLuck!.


Edit for answer to @Bhuvi , (set destination downloaded file)

  1. Define file destination :

            String fileName ="file name";
            File root = android.os.Environment.getExternalStorageDirectory();
            File dir = new File(root.getAbsolutePath() +`subfolder name`);
    
            if (dir.exists() == false) {
                dir.mkdirs();
            }
    
            final Uri destinationUri = Uri.parse(dir + fileName);
    
  2. then setDestinationURI(your path) for ThinDownloadManager

    downloadRequest = 
      new DownloadRequest(downloadUri)setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
    

Edit @farhad.kargaran answer - 2017/12/06

as i saw ThinDownloadManager repository , last version is 1.3.0 but According to @farhad.kargaran answer there are version 1.4.0 too , i have not tested new version's features ,but you can also test the new version according to @farhad.kargaran's answer.




回答2:


Assuming you don't have a custom adapter and you're using something similar to AndroidListClient.java supplied in that tutorial (this assumption is based on you're reference to the article for the code sample).

So, instead of:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list, initialList);
this.setListAdapter(adapter);

create your own custom adapter extending from Base Adapter. (If you already have a custom adapter please post the code).

In your adapter, use viewHolder pattern and don't inflate your list each time item is added.

You can find more about custom adapter here: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown_example




回答3:


Had the same problem. Here is how i solved it (i no longer have the code so we'll have to make due): (I had a database so it was relatively simple to manage the downloads. gave them the item id)

  1. In your AsyncTask broadcast a intent with the item number and the procentage completed (onProgressUpdate)
  2. Implement a LocalBoradcastReciever in your activity and update the ListView with the new values.

I literally have no idea how to pause/resume the download.

I used a static variable to cancel the download (ex cancel no 7, when reading the bytes for the server if(download == 7) return).

Cheers!




回答4:


use

dependencies {
compile 'com.mani:ThinDownloadManager:1.4.0'
}

but in its github it says the last vesion is 1.3.0 but in fact pause and resume feature is in 1.4.0 version and github readMe is not up to date, pay attention to setDownloadResumable() in the following code

DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
            .addCustomHeader("Auth-Token", "YourTokenApiKey")
            .setRetryPolicy(new DefaultRetryPolicy())
            .setDownloadResumable(true)

and for multipart you should do it in initializing download manager as the following code:

int availableProcessors = getRuntime().availableProcessors();
downloadManager = new ThinDownloadManager(availableProcessors);


来源:https://stackoverflow.com/questions/23720624/multiple-file-download-manager-in-listview-progress-pause-resume-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!