7

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 pic:

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

Saeid
  • 2,261
  • 4
  • 27
  • 59
  • Please add code showing how you add an element to the list. Also, if you don't care about Android SDK <=8 then you could use the DownloadManager, which handles everything for you. – Luca Vitucci May 26 '14 at 14:15
  • i said! - i use from this site codes : http://www.hassanpur.com/blog/2011/04/android-development-downloading-a-file-from-the-web/ – Saeid May 26 '14 at 15:14
  • nobody should care about SDK <=8 nowadays ;-) – shalafi May 27 '14 at 12:08
  • sounds like a problem with your listview adapter, post your code please – kevin May 28 '14 at 10:04
  • @shalafi - my min sdk is 8 - it's problem? – Saeid May 29 '14 at 08:40
  • @kevin , i said in my texts - i used from this site codes : http://www.hassanpur.com/blog/2011/04/android-development-downloading-a-file-from-the-web/ – Saeid May 29 '14 at 08:41
  • well, sdk 8 (Froyo) represents 1% of the android devices, while it is a much older version that lacks functionality like DownloadManager. Is it worth the headaches for 1% of potential users? Not to me. – shalafi May 29 '14 at 09:17
  • you right - but how i should to do? - are you have a ready sample? – Saeid May 29 '14 at 09:24

4 Answers4

7

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.

Saeid
  • 2,261
  • 4
  • 27
  • 59
  • please help me in these issues. 1. couldn't find the jar file. and please if you implemented the download manager in listview so please create a gist of your adapter class. thanks a lot. – Devendra Singh Jul 28 '15 at 12:33
  • @DevendraSingh , not have jar file , unfortuanley outdated - if you use android studio u can add this project via Dependencies in build.gradle(Module:app) - else in eclipse you should download this project compeletly and add to your IDE like library! - Goodluck – Saeid Jul 29 '15 at 06:43
  • ok i implemented this library and its working. also found a jar. if any issue will arise,will discuss with you, if you don't mind. – Devendra Singh Jul 29 '15 at 06:47
  • Hey I am using ThinDownloadManager..I can see it has feature of cancelling a download..but can i delete already downloaded file with this library?Also Can i set the path of destination as a folder in external or internal storage instead of cache directory? – Android Developer Sep 27 '15 at 06:42
  • Thanks a lot..I can see it has feature of cancelling an ongoing download..but can i delete already downloaded file with this library using download id? – Android Developer Sep 27 '15 at 07:04
  • @Bhuvi - well , ehen you set file destinition then can downloaded file - ( may I not understand your purpose!) – Saeid Sep 27 '15 at 07:25
  • In my project i am having listview where i am downloading file with each list item..if i want to delete older downloaded files one way i can delete them is to access the folder of external storage and delete them with the name of file..but can i delete them with this library using download id? – Android Developer Sep 27 '15 at 07:37
  • @Bhuvi - u can get file path via this code : downloadRequest.getDestinationURI() , then remove file with code or manualy. :) – Saeid Sep 27 '15 at 08:16
  • @Saeid The download id which it is returning( downloadid = downloadManager.add(downloadRequest);) is same for each download..it should be different for each download?Did u also face same problem? – Android Developer Sep 27 '15 at 13:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90768/discussion-between-saeid-and-bhuvi). – Saeid Sep 28 '15 at 06:56
  • Hello thindownload manger does not have pause resume functionality ? :( Please do help. – Kishan Soni May 11 '16 at 06:58
  • After spending 2 Hours to other way finally use this, It's completely work for me.. great code dude. – Zala Janaksinh Jan 06 '17 at 10:12
  • resume functionality not working thinManager android any idea how it will workable – Tara May 11 '18 at 12:38
0

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

user3623735
  • 345
  • 2
  • 9
0

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!

  • do u have a sample of your? - too , i don't want that my downloads shown in notification bar! – Saeid May 29 '14 at 08:43
  • Sorry, i don't have a sample :( (changed jobs since then so...). – Cristian Radu May 29 '14 at 11:57
  • Based on your code you should add an id field to ListModel (give it to System.currentTimeMillis() when you start the download). Second modification, don't pass a list of names... Pass the list of objects to the adapter. 3 Also pass to your adapter a list with the ids and procentages of the current downloading items (in the adapter set the progress from that list (if item.id == downloadingItems.id setProgress()..)). 4. Read this http://www.icastell.com/2012/09/using-localbroadcastreceiver.html 5. Use what u just learned to pass the progress and download id from the asyncTask to the activity. – Cristian Radu May 29 '14 at 12:21
  • Never said this is easy. – Cristian Radu May 29 '14 at 12:21
  • please see this link : http://stackoverflow.com/questions/19689127/how-to-update-progressbar-in-a-listview-item --- too , i don't want that show Android Download Manager in action bar; thanks. – Saeid Jun 02 '14 at 11:59
0

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);
Saeid
  • 2,261
  • 4
  • 27
  • 59
farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30