2017-06-17 82 views
0

下载监听器永远不会调用,我也没有获得动态下载链接。Webview下载监听器永远不会调用

我有一个网站,包含下载键,同时点击按钮,它生成下载链接的文档,当我点击下载按钮,它成功地将通过铬直接浏览器下载文件,但使用Web浏览它不是自动下载文件以及不接听任何听众的电话。

webView.loadUrl(WEBSITELINK); 

webView.setWebViewClient(new WebViewClient() { 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 
      Log.v("Url:","shouldOverrideUrlLoading"); 
      return true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 

      // download link generates dynamically by clicking on button 

      webView.loadUrl("javascript:(function(){document.getElementById('download').click();})()"); 

     } 



webView.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(mimeType); 
      //------------------------COOKIE!!------------------------ 
      String cookies = CookieManager.getInstance().getCookie(url); 
      request.addRequestHeader("cookie", cookies); 
      //------------------------COOKIE!!------------------------ 
      request.addRequestHeader("User-Agent", userAgent); 
      request.setDescription("Downloading file..."); 
      request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)); 
      request.allowScanningByMediaScanner(); 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
      request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType)); 
      DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      dm.enqueue(request); 
      Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show(); 
     } 
    }); 

回答

0

setDownloadListener设置当的WebView并不认为该内容可以通过渲染引擎

以获取更多信息来处理此链接setDownloadListener

或本link

+0

有什么办法可以强制完全调用监听器吗? – Nik

0

里面的监听器oncreate方法编写代码

webView = (WebView) findViewById(R.id.webView); 
    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webView.loadUrl("place your link here"); 
    webView.setWebViewClient(new WebViewClient()); 

    webView.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.allowScanningByMediaScanner(); 
      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! 

      DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      dm.enqueue(request); 
      Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded 
        Toast.LENGTH_LONG).show(); 

     } 
    }); 

} 
相关问题