【问题标题】:Download Manager Progress Not Visible in the Notification Area通知区域中看不到下载管理器进度
【发布时间】:2017-10-04 03:47:39
【问题描述】:

我正在使用下载管理器从 url 下载文件。文件下载成功。

问题

文件正在静默下载,通知区域没有通知。

下载管理器在我运行 android 6.0 的设备上显示通知(带有进度条)。在我将设备更新到 android 7.0 后,下载管理器不会在通知区域显示任何通知

这是我的代码

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);

另外添加request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); 对我的情况没有帮助。

构建信息

这是我的 Gradle 构建信息。

minSdkVersion 22
targetSdkVersion 23
compileSdkVersion 25
buildToolsVersion "25.0.2"

【问题讨论】:

    标签: android android-download-manager


    【解决方案1】:

    为什么会发生这种情况?

    问题的原因是在设置中禁用了 Android 下载管理器的通知。 (这是我的 Galaxy S7 的新默认设置 - SM930W8 附带新的 Android N (7.0) 更新)

    解决方案1

    1. 进入设置>应用
    2. 在选项中点击“显示系统应用程序
    3. 找到“下载管理器”并打开它
    4. 点击应用设置下的“通知
    5. 开启允许通知

    完成上述步骤后,上述用于下载通知的代码就可以正常工作了。

    解决方案 2

    上述解决方案不适用于应用程序的一般分发。我们不能告诉用户执行解决方案 1。

    添加您自己的小进度条来显示活动中的下载百分比将使用户知道他/她请求的文件正在下载。

    尽量避免通知,因为如果 android 下载管理器发出相同的通知,那么它是多余的。 (显示通知的默认设置可能会因设备所在的 ROM 而异)

    这里是代码。

    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

    @kunal 提到,添加 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 也有帮助。

    【讨论】:

    【解决方案2】:

    我遇到了类似的问题,文件下载成功,但通知区域中看不到进度。

    问题在于通知的可见性。以下行帮助我解决了它:

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      试试这个

      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);
             }
          }
      });
      

      【讨论】:

      • 您能解释一下为什么您的解决方案有效吗?一个代码被剪断是伟大的!但是,您可以通过为您的解决方案添加解释来进一步改进您的回答事件:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 2015-11-12
      • 1970-01-01
      • 2013-01-25
      • 2017-06-21
      相关资源
      最近更新 更多