Note that there are some explanatory texts on larger screens.

plurals
  1. POAsyncTask with notification
    primarykey
    data
    text
    <p>I try to create an AsyncTask which download a Zip file and display the progress of the download inside a notification.</p> <p>I'm calling : </p> <pre><code>new MyAsyncTask.DownloadTask(context, position,list).execute(0); </code></pre> <p>Which refer to this : </p> <pre><code>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&lt;Integer, Integer, Void&gt; { private NotificationHelper mNotificationHelper; public int position; public ArrayList&lt;HashMap&lt;String, String&gt;&gt; list; public DownloadTask(Context context,int position, ArrayList&lt;HashMap&lt;String, String&gt;&gt; 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(); } } </code></pre> <p>It seems to work pretty well, but when I click on my button to run the AsyncTask, all the system is slowing down and it is impossible to use the tablet anymore until the download is finished. (not very useful when the zip is 100mo). </p> <p>Also, I would like to make it possible to cancel the download, but when I try to do it from my main activity (With something like this : <code>MyAsynctask.cancel(true);</code>) the application crash. So I would like to know if there is a proper way to do that (maybe from the notification to give the best user experience).</p> <p>EDIT: </p> <p>The problem with the lag is solved, thanks to buptcoder with an update time less important :</p> <pre><code>while ((count = input.read(data)) != -1) { total += count; if ((count % 10) == 0) { publishProgress((int) ((total * 100) / ;lenghtOfFile)); } output.write(data, 0, count); } </code></pre> <p>I'm still looking for a way to cancel the notification.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload