Download Manager Progress Not Displayed in Notification Area

I am uploading a file with url using Download Manager. And the file was uploaded successfully.

Problem

The file is downloaded without sound, without notification in the notification area.

The download manager showed a notification (with a progress indicator) on my device running on android 6.0. After I upgraded my device to android 7.0, the download manager does not show notifications in 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); doesn't help my case.

Assembly Information

Here is my Gradle built-in function.

 minSdkVersion 22 targetSdkVersion 23 compileSdkVersion 25 buildToolsVersion "25.0.2" 
0
source share
1 answer

Why is this happening in the first place?

The cause of the problem is the Android Download Manager notification in the settings. (This is the new default for my Galaxy S7 - SM930W8, which comes with the new Android update (7.0).

Solution 1

  • Go to Settings> Application
  • In the settings, click " Show system applications
  • Find ' Download Manager ' and open it
  • Click " Notifications " under "Application Settings"
  • Enable Allow Notifications

After completing the above steps, the above code snippet for downloading with the notification works just fine.

Decision 2

The above solution will not work with the general distribution of the application. We cannot tell users to implement the solution.

Adding your own little progress indicator to show the percentage of downloads in your activity will give the user some idea that the downloaded file is loading.

Try to avoid the notification, because if the android download manager provides the same notification, it will be redundant. (the default setting for show notification may differ from device to device with respect to which they are located)

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(); 
0
source

Source: https://habr.com/ru/post/1270481/


All Articles