Skip to content

Instantly share code, notes, and snippets.

@Ased2235
Created August 5, 2019 17:12
Show Gist options
  • Save Ased2235/0675a49566457af365eb40f20fa1e65f to your computer and use it in GitHub Desktop.
Save Ased2235/0675a49566457af365eb40f20fa1e65f to your computer and use it in GitHub Desktop.
@SuppressLint("StaticFieldLeak")
class DownloadFile extends AsyncTask<String, String, String> {
int id = 1;
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
CountDownTimer cdt;
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
mNotifyManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (mNotifyManager != null) {
mNotifyManager .createNotificationChannel(Objects.requireNonNull(createdownload(mNotifyManager )));
}
}
mBuilder = new NotificationCompat.Builder(context,DOWNLOAD_CHANNEL_ID);
mBuilder.setContentTitle("Downloading File")
.setContentText("name")
.setProgress(0, 100, false)
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_LOW);
// mBuilder.setProgress(100, 0, false);
}
protected String doInBackground(String... requestObjects) {
String f_url = requestObjects[0];
String fileName = "aa.pdf";
int count;
Log.d(TAG, "URL: "+ f_url);
try {
URL url = new URL(f_url);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder)
.setSummaryText(getStringSizeLengthFile(lenghtOfFile)));
Log.d(TAG, "leam: "+getStringSizeLengthFile(lenghtOfFile));
mNotifyManager.notify(id,mBuilder.build());
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
// OutputStream output = new FileOutputStream("/sdcard/"+f_url[i]);
OutputStream output = new FileOutputStream(
folder.getAbsolutePath()+"/"+ fileName);
Log.d(TAG,"out: "+folder.getAbsolutePath()+"/"+ fileName);
byte[] data = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e(TAG,"Error: ", e);
}
return null;
}
/**
* Updating progress bar
*/
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onProgressUpdate(String... values) {
// Update progress
mBuilder.setProgress(100, Integer.parseInt(values[0]), false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment