Download Manager Progress Not Visible in the Notification Area
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
- Go to the Settings > App
- In the options click 'Show System Apps'
- Find 'Download Manager' and open it
- Click 'Notifications' under App Settings
- Switch on Allow Notifications
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.
On progress seekbar notification not showing in DownloadManager android
Yes I found solution. The problem is didn't enabled download manager.
Go to the Settings > App
In the options click 'Show System Apps'
Find 'Download Manager' and open it
Click 'Notifications' under App Settings
Switch on Allow Notifications
Reference:
https://stackoverflow.com/a/43841708/1921263
Message from DownloadManager in closed NotificationBar in Android
Notification overview. Method notify. Method cancel.
showing download progress in progress bar using DownloadManager
You have a Logic error, Your Query is out of the while loop where the UI is updated
private void startAppDownload() {
new Thread(new Runnable() {
@Override
public void run() {
boolean isDownloading = true;
int downloadStatus, totalBytesDownloaded, totalBytes;
while (isDownloading) {
DownloadManager.Query downloadQuery = new DownloadManager.Query();
downloadQuery.setFilterById(downloadID);
Cursor cursor = downloadManager.query(downloadQuery);
cursor.moveToFirst();
totalBytesDownloaded = cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
);
totalBytes = cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
);
downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
isDownloading = false;
break;;
}
final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);
runOnUiThread(new Runnable() {
@Override
public void run() {
downloadProgressBar.setProgress(downloadProgress);
}
});
}
cursor.close();
}
}).start();
}
Is there any way to show 'download completed' notification using react-native?
according to https://github.com/cjdell/react-native-fs-test/blob/master/index.common.js
const ret = RNFS.downloadFile({ fromUrl: url, toFile: downloadDest });
ret.promise.then(res => {
// Show Your Notification
}).catch(err => {});
for notification you can use https://github.com/zo0r/react-native-push-notification, this is an example:
RNFS.downloadFile({
fromUrl,
toFile,
}).promise.then((res) => {
PushNotification.localNotification(details)
});
Related Topics
How to Place an Imageview on Top of Another Imageview in Android
Where Is Android_Sdk_Root and How to Set It.
How to Check If Data Is Inserted in Room Database
How to Set Android_Ndk_Home So That Android Studio Does Not Ask for Ndk Location
How to Disable All User Inputs (Click, Touch) on an Android View
Android - How to Make a Button Click Play a Sound File Every Time It Been Pressed
How to Avoid Overlap View in Relative Layout in Android
How to Synchronously Load Data from Firebase
Showing Thumbnail for Link in Whatsapp || Og:Image Meta-Tag Doesn't Work
Android Studio Inline Compiler Showing Red Errors, But Compilation With Gradle Works Fine
How to Remove Android Auto-Suggest Underlining in Edittext
How to Get Context in Android Mvvm Viewmodel
How to Fix Mainactivity Does Not Have a Navcontroller
Making Voice Calls from Android Phone Connected to Linux
Android.Support.V7.App.Activitycompat Not Found
Android Volley Basicnetwork.Performrequest() Unexpected Response Code 404
How Capture/Log API Calls (Function Calls) Made by Android App