DownloadListener.onDownloadStart() never called

十年热恋 提交于 2019-12-19 07:27:34

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5797976/downloadlistener-ondownloadstart-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!