android - Will AsyncTask crash if I call it over 100 times in a loop? -


i getting list of files web service , using asynctask handle download of files. each task created in loop:

for(int = 0; < arraysize; i++) { //omitted code except portion fires asynctask      //download pdf     downloadhandler dhpdf = new downloadhandler(cleanlink, filename, p1, downloadposters.this, "pdf");     dhpdf.startdownload();      //download png     downloadhandler dhpng = new downloadhandler(cleanlink, filename, p1, downloadposters.this, "png");     dhpng.startdownload();  } 

the download class

public class downloadhandler extends activity {  private context mcontext; //file ====================== public string filename; private string remotepath; private file file; private string ext; //progress bar ============== private progressbar progressbar; private int progressstatus = 0; //private handler handler = new handler(); private textview mtextview; //progressdialog ============ progressdialog mprogress; private int mprogressdialog=0;   public downloadhandler(string rp, string f, progressbar progressbar, context c, string extension, textview textview) throws exception {     mcontext = c;     remotepath = rp;     filename = f;     file = new file(mcontext.getfilesdir(), filename+"."+extension);     ext = extension;     this.progressbar = progressbar;     mtextview = textview; }  //our method public void startdownload() {     string url = "http://examplesite.com/"+remotepath+"/"+filename+"."+ext;      new downloadfileasync().execute(url); }  class downloadfileasync extends asynctask<string, integer, string> {      @override     public void onpreexecute() {      }      @override     protected void onprogressupdate(integer... values) {         progressbar.setprogress(values[0]);         mtextview.settext("downloading: "+ext);      }      @override     protected string doinbackground(string... aurl) {          int count;          try {             url url = new url(aurl[0]);              urlconnection conection = url.openconnection();             conection.connect();             // music file length             int lenghtoffile = conection.getcontentlength();             // input stream read file - 8k buffer             inputstream input = new bufferedinputstream(url.openstream(),10*1024);              // output stream write file in internal storage             outputstream output = new bufferedoutputstream(new fileoutputstream(file));              byte data[] = new byte[1024];             long total = 0;             while ((count = input.read(data)) != -1) {                 total += count;                 // publish progress triggers onprogressupdate method                 publishprogress((int) ((total * 100) / lenghtoffile));                  // write data file                 output.write(data, 0, count);             }             // flush output             output.flush();             // close streams             output.close();             input.close();          } catch (exception e) {mtextview.settext("error:"+e.tostring());}         return null;      }      @override     protected void onpostexecute(string unused) {         mtextview.settext("complete");      } } 

right able test 6 files , seems working well.

my question if proper way que multiple downloads , can handle 100+ files @ time without crashing?

why not using android download manager ? can queue of download requests, , it's service, work in background. checks connection, resumes downloads when connection gone , re established.

for more information , quick start, check tutorial. should help. vogella blog


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -