How To Prevent Ui Lag When Updating Notification While Downloading File?
Solution 1:
so how could I go about changing the background code to call publishProgress only every second
I have done this before for an upload function that showed the % in a Notification
, but same exact idea. Have your AsyncTask
keep track of what percentDone
the download is, and ONLY call publishProgress
when percentDone
changes. That way, you will only ever call publishProgress
when the % downloaded changes, and the Notification
therefore needs to update. This should resolve the UI lag.
I was writing this up as my suggested implementation, sounds like the OP already got it working. But maybe this will help someone else in the future:
byte data[] = newbyte[1024];
long total = 0;
int count, latestPercentDone;
int percentDone = -1;
while ((count = input.read(data)) != -1) {
total += count;
latestPercentDone = (int) Math.round(total / fileLength * 100.0);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
publishProgress(percentDone);
}
output.write(data, 0, count);
}
Solution 2:
I really liked your approach to it! I found that changing the code to the following made my progressbar update correctly. I had some issues while using math.round().
latestPercentDone = (int) ((dataBytesWritten / (float) totalSize) * 100);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
publishProgress(percentDone);
}
Post a Comment for "How To Prevent Ui Lag When Updating Notification While Downloading File?"