Download Manager Progress Not Visible in the Notification Area

会有一股神秘感。 提交于 2019-12-12 04:45:13

问题


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"

回答1:


I was facing a similar problem that the file was downloading successfully but the progress was not visible in the notification area.

The issue was with notification visibility. Following line helped me solve it:

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

I hope this helps.




回答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

  1. Go to the Settings > App
  2. In the options click 'Show System Apps'
  3. Find 'Download Manager' and open it
  4. Click 'Notifications' under App Settings
  5. 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 a application. We can't tell the users to do the solution 1.

Adding your own small progressbar 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();



回答3:


try this

DownloadPdf = (Button) findViewById(R.id.downloadpdf);
DownloadPdf.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

       File file = new File (Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOWNLOADS)+"/Pdf/" + "/" + name + ".pdf");
        if (file.exists()) {
            Toast.makeText(CatalogDetailActivity.this, "File Sudah ada", Toast.LENGTH_SHORT).show();
        } else {

      Download_Uri = Uri.parse(testing);
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        request.setAllowedOverRoaming(false);
        request.setTitle("Downloading");
        request.allowScanningByMediaScanner();
        request.setDescription("Downloading");

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "/Pdf/" + "/" + name + ".pdf");
        refid = downloadManager.enqueue(request);
       }
    }
});


来源:https://stackoverflow.com/questions/43808716/download-manager-progress-not-visible-in-the-notification-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!