android - AsyncTask with notification -


i try create asynctask download zip file , display progress of download inside notification.

i'm calling :

new myasynctask.downloadtask(context, position,list).execute(0); 

which refer :

import java.io.bufferedinputstream; import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import java.net.url; import java.net.urlconnection; import java.util.arraylist; import java.util.hashmap;  import android.annotation.suppresslint; import android.content.context; import android.os.asynctask; import android.os.environment; import android.util.log; import android.widget.button;  public class downloadtask extends asynctask<integer, integer, void> {     private notificationhelper mnotificationhelper;      public int position;     public arraylist<hashmap<string, string>> list;      public downloadtask(context context,int position, arraylist<hashmap<string, string>> list) {         mnotificationhelper = new notificationhelper(context);         this.position = position;         this.list = list;     }      protected void onpreexecute() {         mnotificationhelper.createnotification();     }      @suppresslint("newapi")     @override     protected void doinbackground(integer... integers) {              int count;             try {                 url url = new url("http://myurl/test.zip");                 urlconnection conection = url.openconnection();                 conection.connect();                 int lenghtoffile = conection.getcontentlength();                 log.d("size: ", integer.tostring(lenghtoffile));                  inputstream input = new bufferedinputstream(url.openstream(),8192);                  outputstream output = new fileoutputstream(environment.getexternalstoragedirectory().getpath()+ "/"+ list.get(position).get("name") + ".zip");                  byte data[] = new byte[1024];                  long total = 0;                  while ((count = input.read(data)) != -1) {                     total += count;                     publishprogress((int) ((total * 100) / lenghtoffile));                     output.write(data, 0, count);                 }                  output.flush();                 output.close();                 input.close();              } catch (exception e) {                 log.e("error: ", e.getmessage());             }          return null;     }      @override     protected void onprogressupdate(integer... progress) {         mnotificationhelper.progressupdate(progress[0]);     }      @override     protected void onpostexecute(void result) {         mnotificationhelper.completed();     } } 

it seems work pretty well, when click on button run asynctask, system slowing down , impossible use tablet anymore until download finished. (not useful when zip 100mo).

also, make possible cancel download, when try main activity (with : myasynctask.cancel(true);) application crash. know if there proper way (maybe notification give best user experience).

edit:

the problem lag solved, buptcoder update time less important :

while ((count = input.read(data)) != -1) {     total += count;     if ((count % 10) == 0) {         publishprogress((int) ((total * 100) / ;lenghtoffile));     }     output.write(data, 0, count); } 

i'm still looking way cancel notification.

you update notification fast. have decrease frequency of update.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -