I am trying to use a AsyncTasknotification file to upload. Everything works fine (loading in the background thread), except when I add the code to update the progress bar through publishProgress(), it freezes the entire phone until the download is complete and the notification shows โComplete Downloadโ.
I completely lost my opinion on why this is so, but maybe I think about it in the lines publishProgress((downloadedSize / totalSize) * 100)that I use?
In any case, this is DownloadDataTask:
protected String doInBackground(RomDataSet... params) {
try {
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
publishProgress((downloadedSize / totalSize) * 100);
}
fileOutput.close();
} 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);
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.setTextViewText(R.id.download_description, Integer.toString(progress[0]));
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);
}
I am really stuck with this - any help would be appreciated. Greetings
@Override
protected void onProgressUpdate(Integer... progress) {
if ((progress[0] - lastSize) > 5000) {
lastSize = progress[0];
notification.contentView.setProgressBar(R.id.download_progress, totalSize, progress[0], false);
notificationManager.notify(43, notification);
}
}