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?