DownloadListener.onDownloadStart() 从未调用过
在我尝试创建一个通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setDownloadListener 当 WebView 认为渲染引擎无法处理内容时设置侦听器。
webview使用WebKit渲染引擎,我相信它可以(或认为它可以)处理html5,这样监听器就不会被调用。
setDownloadListener sets the listener when the WebView doesn't think the content can be handled by the rendering engine.
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.