Freezing UI Thread with AsyncTask
<p>I'm trying to use an <code>AsyncTask</code> to download a file with a notification progress. Everything works fine (downloads in background thread), except whenever I add the code to update the progress bar through <code>publishProgress()</code>, it freezes the whole phone, until the download is finished and the notification shows "Download Completed".</p>

<p>I'm completely lost as to why this is the case, but I am maybe thinking it's along the lines of the <code>publishProgress((downloadedSize / totalSize) * 100)</code> that i'm using?</p>

<p>Anyways here is the <code>DownloadDataTask</code>:</p>

<pre><code>    protected String doInBackground(RomDataSet... params) {
        try {

            // Download file here, all good

            //now, read through the input buffer and write the contents to the file
            while ( (bufferLength = inputStream.read(buffer)) &gt; 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //this is where you would do something to report the prgress, like this maybe

                publishProgress((downloadedSize / totalSize) * 100);
            }
            //close the output stream when done
            fileOutput.close();

        //catch some possible errors...
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        Intent intent = new Intent(ListActivity.this, ListActivity.class);
        pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        // configure the notification
        notification = new Notification(R.drawable.ic_stat_rom, "Downloading Rom via RomGet", System
                .currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.layout_download);
        notification.contentIntent = pendingIntent;
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon_rom);
        notification.contentView.setTextViewText(R.id.download_description, "Downloading");
        notification.contentView.setProgressBar(R.id.download_progress, 100, 0, false);

        getApplicationContext();
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                Context.NOTIFICATION_SERVICE);

        notificationManager.notify(43, notification);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        //notification.contentView.setProgressBar(R.id.download_progress, 100, Math.round(progress[0]), false);
        notification.contentView.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
        // inform the progress bar of updates in progress
        notificationManager.notify(43, notification);
    }

    @Override
    protected void onPostExecute(String result) {
        notification.contentView.setProgressBar(R.id.download_progress, 100, 100, false);
        notification.contentView.setTextViewText(R.id.download_description, "Done");
        notificationManager.notify(43, notification);
    }
</code></pre>

<p>I'm really stuck on this one - Any help would be appreciated. Cheers</p>

<pre><code>    @Override
    protected void onProgressUpdate(Integer... progress) {
        if ((progress[0] - lastSize) &gt; 5000) {
            lastSize = progress[0];
            notification.contentView.setProgressBar(R.id.download_progress, totalSize, progress[0], false);
            //notification.contentView.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
            // inform the progress bar of updates in progress
            notificationManager.notify(43, notification);
        }
    }
</code></pre>
