在我尝试创建通过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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
由于某些原因它没有被调用,因此注意到我的代码中没有任何地方指定"implements DownloadListener",我将其重新实现为一个单独的类,定义为
public class MyDownloadListener implements DownloadListener
Run Code Online (Sandbox Code Playgroud)
并如上所述实现onDownloadStart()(将活动作为参数传递,以便它可以调用startActivity().然后在onCreate()中,我只是这样做:
mDownloadListener = new MyDownloadListener(this);
mWebView.setDownloadListener(mDownloadListener); …Run Code Online (Sandbox Code Playgroud)