android - DownloadListener.onDownloadStart() 从未调用过

标签 android html webview android-webview

在我尝试创建一个通过 HTML5(并且 通过 Flash)播放 YouTube 视频的 WebView 时,我尝试实现 this article逐字记录,就在我 Activity 的 onCreate() 中:

  WebView webView = (WebView) findViewById(R.id.embeddedWebView);
  webView.setDownloadListener(new DownloadListener()
  {
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long size)
    {
      Log.v("TAG", "url: " + url + ", mimeType: " + mimeType);

      Intent viewIntent = new Intent(Intent.ACTION_VIEW);
      viewIntent.setDataAndType(Uri.parse(url), mimeType);

      try
      {
        startActivity(viewIntent);
      }
      catch (ActivityNotFoundException ex)
      {
        Log.w("YourLogTag", "Couldn't find activity to view mimetype: " + mimeType);
      }
    }
  });  

由于某种原因它没有被调用,所以注意到我的代码中没有任何地方指定“implements DownloadListener”,我将它重新实现为一个单独的类,定义为

public class MyDownloadListener implements DownloadListener 

并如上所述实现 onDownloadStart()(将 Activity 作为参数传递,以便它可以调用 startActivity()。然后在 onCreate() 中,我只需执行以下操作:

mDownloadListener = new MyDownloadListener(this);
mWebView.setDownloadListener(mDownloadListener);

我在 YouTube 上再次尝试了 http://broken-links.com/tests/video/而且我仍然没有在 LogCat 中看到任何调用过 onDownloadStart() 的痕迹。

我需要做什么才能调用它?我错过了什么?

最佳答案

setDownloadListener 在 WebView 认为渲染引擎无法处理内容时设置监听器。

Register the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead. This will replace the current handler.

webview 使用WebKit 渲染引擎,我相信可以(或认为可以)处理html5,因此不会调用listener。

关于android - DownloadListener.onDownloadStart() 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5797976/

相关文章:

java - 如果从 webview ~Android ~Java 打开,网页内的链接总是返回 "404 not found"错误

android - 使用 PDF 链接 Action_view 让 Webview Activity 消失

android - 广播接收器和未决 Intent : Show a toast

android - Facebook + 安卓 : Only fullscreen opaque activities can request orientation

html - 赋予多个字体家族名称的意义是什么

c# - 从Windows Store应用程序中的WebView打印静态内容

android - map 安卓 API : Authorization failure

android - 我如何设置 android 微调器提示的样式

html - 像图像一样缩放 div

javascript - 通过 angular.js 中的作用域变量显示或附加元素