首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载管理器进度在“通知”区域中不可见

下载管理器进度在“通知”区域中不可见
EN

Stack Overflow用户
提问于 2017-05-05 15:29:16
回答 3查看 15.4K关注 0票数 2

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

问题

文件正在悄悄下载,通知区域中没有通知。

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

这里是我的代码

代码语言:javascript
复制
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构建信息。

代码语言:javascript
复制
minSdkVersion 22
targetSdkVersion 23
compileSdkVersion 25
buildToolsVersion "25.0.2"
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-08 07:08:22

,为什么会发生这种情况?

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

解决方案1

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

在完成上述步骤之后,上面的下载代码就可以正常工作了。

解决方案2

上面的解决方案不适用于应用程序的一般分布。我们不能告诉用户做解决方案1。

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

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

这是密码。

代码语言:javascript
复制
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

正如库纳尔提到的那样,添加request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);也有帮助。

票数 8
EN

Stack Overflow用户

发布于 2018-09-11 08:40:12

我正面临一个类似的问题,即文件正在成功下载,但在通知区域中看不到进度。

问题是通知的可见度。以下几句话帮助我解决了这个问题:

代码语言:javascript
复制
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

我希望这能帮到你。

票数 10
EN

Stack Overflow用户

发布于 2018-06-12 08:57:50

尝尝这个

代码语言:javascript
复制
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);
       }
    }
});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43808716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档