NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
///////////// ایجاد نوتیفیکیشن دانلود
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(
Activity.this);
mBuilder.setContentTitle("دانلود فایل - کدآموز")
.setContentText("درحال دانلود...").setSmallIcon(R.drawable.app_logo)
.setProgress(100, , false);
mBuilder.setOngoing(true);
mNotifyManager.notify(1, mBuilder.build());
/////////////// کد های کتابخانه دانلود فایل
ThinDownloadManager downloadManager = new ThinDownloadManager();
String URI = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; // لینک فایل
String saveFile = "/video.mp4"; // اسم فایل دانلودی
Uri downloadUri = Uri.parse(URL);
///////// محل ذخیره
Uri destinationUri = Uri.parse(this.getExternalCacheDir().toString() + saveFile);
DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
// .addCustomHeader("Auth-Token", "YourTokenApiKey")
.setRetryPolicy(new DefaultRetryPolicy())
.setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
.setDownloadContext(getApplicationContext())//Optional
.setDownloadListener(new DownloadStatusListener() {
@Override
public void onDownloadComplete(int id) {
//// اگر دانلود تمام شد...
Intent notificationIntent = new Intent(Activity.this, NextActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(NextActivity.this, id, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder.setContentTitle("دانلود فایل - کدآموز")
.setContentText("دانلود کامل شد.").setSmallIcon(R.drawable.app_logo)
.setContentIntent(contentIntent);
// Issues the notification
mNotifyManager.notify(1, mBuilder.build());
}
@Override
public void onDownloadFailed(int id, int errorCode, String errorMessage) {
/// اگر دانلود ناموفق بود ...
}
@Override
public void onProgress(int id, long totalBytes, long downlaodedBytes, int progress) {
// Issues the notification
///// آپدیت کردن نوتیفیکیشن بار ...
mBuilder.setProgress(100, progress, false).setContentText(progress + "%");
mNotifyManager.notify(ID, mBuilder.build());
}
});
downloadManager.add(downloadRequest);
عالی بود ممنون
عالی