downloading - Android: Download thread stucks the app -


my android application gets stuck when downloading file until downloading finished. however,the downloading thread inherited aynctask , in background. can have , see wrong , how can modify code in order work?

    private class downloadfiletask extends asynctask<string, integer, string> {      file destfile;      private boolean openafterdownload;     private exception failure;      public downloadfiletask(boolean openafterdownload) {         this.openafterdownload = openafterdownload;     }      @override     protected void onpreexecute() {         super.onpreexecute();                    downloaddialog.setmessage(getstring(r.string.downloading));         downloaddialog.show();     }      @override     protected string doinbackground(string... params) {          try {              string url = params[0];             log.debug("downloading: " + url);              string filename = url.substring(url.lastindexof('/') + 1);              httpparams httpparams = new basichttpparams();             defaulthttpclient client = new defaulthttpclient(httpparams);             client.getcredentialsprovider().setcredentials(                     new authscope(null, -1),                     new usernamepasswordcredentials(user, password));             httpget = new httpget(url);              httpresponse response = client.execute(get);              if (response.getstatusline().getstatuscode() == 200) {                  file destfolder = new file(config.getdownloadsfolder());                 if (!destfolder.exists()) {                     destfolder.mkdirs();                 }                  /**                  * make sure store downloaded files .epub,                   * show in scans later on.                  */                 if ( ! filename.endswith(".epub") ) {                     filename = filename + ".epub";                 }                  destfile = new file(destfolder, urldecoder.decode(filename));                  if (destfile.exists()) {                     destfile.delete();                 }                  // lenghtoffile used calculating download progress                 long lenghtoffile = response.getentity().getcontentlength();                 if(lenghtoffile>=config.getavailablespace())                 {                     this.failure = new exception("not enough space");                     return null;                 }                 // file seen after download                 fileoutputstream f = new fileoutputstream(destfile);                  try {                     // file input url                     inputstream in = response.getentity().getcontent();                      // here's download code                     byte[] buffer = new byte[1024];                     int len1 = 0;                     long total = 0;                      while ((len1 = in.read(buffer)) > 0) {                          // make sure user can cancel download.                         if (iscancelled()) {                             return null;                         }                          total += len1;                         publishprogress((int) ((total * 100) / lenghtoffile));                         f.write(buffer, 0, len1);                     }                 } {                     f.close();                 }              } else {                 this.failure = new runtimeexception(response                         .getstatusline().getreasonphrase());                 log.error("download failed: "                         + response.getstatusline().getreasonphrase());             }          } catch (exception e) {             toast.maketext(getactivity(), e.getmessage() + "1",                     toast.length_long).show();             log.error("download failed.", e);             this.failure = e;         }          return null;     } 

although running task in asynctask have lines of code:

    downloaddialog.setmessage(getstring(r.string.downloading));     downloaddialog.show(); 

in onpreexecute() method, start dialogbox before task self starts.

what more don't see onpostexecute() method in implementation. never .dismiss() dialog. if did application still 'stack' (normal behavior) dialog until download finishes.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -