从未调用过DownloadListener.onDownloadStart() [英] DownloadListener.onDownloadStart() never called

查看:504
本文介绍了从未调用过DownloadListener.onDownloadStart()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我尝试创建通过 HTML5 播放YouTube视频的WebView(以及不是通过Flash)时,我尝试实现这篇文章一字一句,就在我的活动的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,找不到查看mimetype的活动:+ mimeType);
}
}
});

由于某些原因没有调用它,因此注意到我的代码中没有任何地方可以指定实现了DownloadListener,我将它重新实现为一个单独的类,定义为

 公共类MyDownloadListener实现DownloadListener 

并如上所述实现onDownloadStart()(将活动作为参数传递,以便它可以调用startActivity()。然后在onCreate中(),我只是这样做:

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

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



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


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


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


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?

解决方案

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.

这篇关于从未调用过DownloadListener.onDownloadStart()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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