我想在加载网页后 3 秒后开始播放视频。这是我的 html 代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="3;url=http://xx.xxx.x.xxx:8080/small.mp4"/>
</head>
<body>
<ul>
<li><a title="Home" alt="Home" href="/">Home</a></li>
<li><a title="Match Centre" alt="Match Centre" href="/menu/centre">Match Centre</a></li>
<li><a title="Clubs" class="more clubs" href="/menu/clubs"><span>Clubs</span></a></li>
<li><a title="Menu" class="more menu" href="/menu/options"><span>Menu</span></a></li>
</ul>
</body>
</html>
它在我尝试过的所有浏览器(包括 Android noraml 浏览器)中都运行良好,但不适用于 Android 嵌入式浏览器。
这是我的主要 Activity :
public class MainActivity extends Activity {
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
Log.e("CEmbeddedBrowser eceivedError()", "Fail to load: " + failingUrl);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
});
webView.loadUrl("http://xx.xxx.x.xxx:8080/mypage.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Android 文档说 WebViewClient 支持元标记。我已尝试使用普通页面 (http://www.google.com">) 进行重定向并且它有效,但我不知道为什么它不适用于视频。
最佳答案
对于像我一样寻找解决方案的任何其他人,事实证明我必须将 WebViewClient 指定为导航目标。元刷新/重定向似乎被视为导航 Activity ,所以只要我告诉 webview 在其内部处理导航,它就可以完美地工作。
Google Developer Documentation :
Handling Page Navigation
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that's maintained by your WebView.
To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:
WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.setWebViewClient(new WebViewClient());
关于android - 使用 http-equiv ="refresh"元标记重定向到视频在 Android 嵌入式浏览器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345815/