I am currently using AsyncTask
to download a large file in the background within my app, currently the download progress is shown as a ProgressDialog
which is updated via onProgressUpdate
as below:
protected String doInBackground(String... sUrl) {
try {
String destName = sUrl[1];
file_Delete(destName); // Just to make sure!
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destName);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e(TAG, NAME + ": Error downloading file! " + e.getMessage());
return e.getMessage();
}
return null;
}
@Override protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
DownloadImage.mProgressDialog.setProgress(progress[0]);
}
This works fine, however I now want to use a notification in the notification bar so keep track of the download instead (as the file can be rather large and users would like to keep track from outside the app).
I have tried the below code however the UI starts to lag badly, I can see its due to the publishProgress
getting called alot, so how could I go about changing the background code to call publishProgress
only every second
@Override protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
DownloadImage.mProgressDialog.setProgress(progress[0]);
DownloadImage.myNotification = new NotificationCompat.Builder(c)
.setContentTitle("Downloading SlapOS")
.setContentText("Download is " + progress[0] + "% done")
.setTicker("Downloading...")
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setProgress(100, progress[0], false)
.setSmallIcon(R.drawable.icon)
.build();
DownloadImage.notificationManager.notify(1, DownloadImage.myNotification);
}