I am downloading a file from url using Download Manager. And the file is downloaded successfully.
The problem
File is downloading silently, no notification in the notification area.
The download manager showed notification (with progressbar) on my device running on android 6.0. After i have updated my device to android 7.0 the download manager doesn't show any notification on the notification area.
Here is my code
Uri uri = Uri.parse("file://" + destination);
url = "http:....."; //Valid File URL
//Set up download manager request
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading " + file_name);
request.setTitle("My Downloader");
request.setDestinationUri(uri); //URI is valid
//Start the download
DownloadManager manager = (DownloadManager) getContext()
.getSystemService(Context.DOWNLOAD_SERVICE);
Also adding request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
is not helping my case.
Build Information
Here is my Gradle Build infformation.
minSdkVersion 22
targetSdkVersion 23
compileSdkVersion 25
buildToolsVersion "25.0.2"
Why This Happens in the first place?
The cause of the problem is the Android Download Manager's Notification was disabled in the settings. (It is the new default for my Galaxy S7 - SM930W8 which comes with the new Android N (7.0) Update)
The solution 1
After completing the above steps the above piece of code for downloading with notification working just fine.
The Solution 2
The above solution won't work with the general distribution of an application. We can't tell the users to do solution 1.
Adding your own small progress bar to show downloading percentage in your activity will give the user some idea that the file he/she requested is downloading.
Try to avoid notification because if the android download manager is giving the same notification then it is redundant. (default setting of show notification may vary device to device with respect to which ROM they are on)
So here is the code.
Uri uri = Uri.parse("file://" + destination);
url = "http:....."; //Valid File URL
//Set up download manager request
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading " + file_name);
request.setTitle("My Downloader");
request.setDestinationUri(uri); //URI is valid
//Start the download
DownloadManager manager = (DownloadManager) getContext()
.getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
final int UPDATE_PROGRESS = 5020;
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==UPDATE_PROGRESS){
String downloaded = String.format("%.2f MB", (double)((msg.arg1)/1024)/1024);
String total = String.format("%.2f MB", (double) (msg.arg2)/1024)/1024);
String status = downloaded + " / " + total;
pDialog.setTitleText(status);
}
super.handleMessage(msg);
}
};
new Thread(new Runnable() {
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
//Post message to UI Thread
Message msg = handler.obtainMessage();
msg.what = UPDATE_PROGRESS;
//msg.obj = statusMessage(cursor);
msg.arg1 = bytes_downloaded;
msg.arg2 = bytes_total;
handler.sendMessage(msg);
cursor.close();
}
}
}).start();
The Solution 3
Adding request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
also helps as @kunal mentioned.