进度搜寻栏通知未显示在DownloadManager Android中 [英] On progress seekbar notification not showing in DownloadManager android

查看:766
本文介绍了进度搜寻栏通知未显示在DownloadManager Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Webview下载文件.但是在使用DownloadManager时,它不会显示类似于的正在进行的下载通知.它只是在后台运行. 下载进行中时如何显示状态栏通知. 我可以获取下载的文件,但是如何在通知中显示正在进行的进程?

I am downloading the file from webview. But it's not displaying the on going download notification similar to this when using DownloadManager. It just doing in background operation. How to display the status bar notification when downloading is in progress. I can able to get the downloaded file but how to display the on going process in notification?

我使用了"request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);"但它没有显示正在进行的过程.请指导我我正在做什么错误.

I used "request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);" but it's not displaying the on going process. Please guide me what mistake I am doing.

这是我使用的代码.

mWebview.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));
                request.setMimeType("pdf");


                String cookies = CookieManager.getInstance().getCookie(url);


                request.addRequestHeader("cookie", cookies);


                request.addRequestHeader("User-Agent", userAgent);


                request.setDescription("Downloading file...");


                request.setTitle(URLUtil.guessFileName(url, contentDisposition,
                        "pdf"));


                request.allowScanningByMediaScanner();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      //clueless why it's not working..    
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
                } else {
                    request.setShowRunningNotification(true);
                }

                //request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(
                        Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
                                url, contentDisposition, "pdf"));
                dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                downloadReference = dm.enqueue(request);
                IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
                registerReceiver(downloadReceiver, filter);
            }
        });
    }

    private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            if (downloadReference == referenceId) {
                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
                Cursor c = dm.query(q);

                if (c.moveToFirst()) {
                    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    if (status == DownloadManager.STATUS_SUCCESSFUL) {
                        // process download
                        String title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
                        File file = new File(Environment.getExternalStorageDirectory()
                                + "/Download/" + title);//name here is the name of any string you want to pass to the method
                        if (!file.isDirectory())
                            file.mkdir();
                        //Intent testIntent = new Intent("com.adobe.reader");
                        Intent testIntent = new Intent(Intent.ACTION_VIEW);
                        testIntent.setType("application/pdf");
                        //testIntent.setAction(Intent.ACTION_VIEW);
                        Uri uri = Uri.fromFile(file);
                        testIntent.setDataAndType(uri, "application/pdf");
                        try {
                            startActivity(testIntent);
                        } catch (ActivityNotFoundException e) {
                            Toast.makeText(MainActivity.this, "No application available to view PDF",
                                    Toast.LENGTH_SHORT).show();
                        }
                        // get other required data by changing the constant passed to getColumnIndex
                    }
                }
            }
        }
    };

我在Android 7.1.1中签入.

I checked in Android 7.1.1.

推荐答案

是的,我找到了解决方案.问题是未启用下载管理器.

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

参考:

https://stackoverflow.com/a/43841708/1921263

这篇关于进度搜寻栏通知未显示在DownloadManager Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆