DownloadListener.onDownloadStart() 从未调用过

发布于 2024-11-03 12:21:48 字数 1501 浏览 1 评论 0原文

在我尝试创建一个通过 HTML5(而不是通过 Flash)播放 YouTube 视频的 WebView 时,我尝试实现 这篇文章逐字,就在我的活动的 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);
      }
    }
  });  

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

public class MyDownloadListener implements DownloadListener 

并实现 onDownloadStart() ,如上所述(将活动作为参数传递,以便它可以调用然后在 onCreate() 中,我简单地执行以下操作:

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

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

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

In my attempts to create a WebView that plays YouTube videos via HTML5 (and not via Flash), I tried to implement this article verbatim, right inside my activity's 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);
      }
    }
  });  

It didn't get called for some reason, so noticing that nowhere in my code do I specify "implements DownloadListener", I re-implemented it as a separate class that's defined as

public class MyDownloadListener implements DownloadListener 

and implements onDownloadStart() as above (passing the activity as a parameter so that it can call startActivity(). Then in onCreate(), I simply do:

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

I tried again on YouTube and on http://broken-links.com/tests/video/ and I still don't see in LogCat any trace that onDownloadStart() is ever being called.

What do I need to do to have it called? What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-11-10 12:21:48

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

注册当渲染引擎无法处理内容并应下载内容时要使用的接口。这将替换当前的处理程序。

webview使用WebKit渲染引擎,我相信它可以(或认为它可以)处理html5,这样监听器就不会被调用。

setDownloadListener sets the listener when the WebView doesn't think the content can be handled by the rendering engine.

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.

The webview uses the WebKit rendering engine and I believe that can (or thinks it can) handle html5 so that listener will not be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文