我正在尝试在android中实现paytab支付,它使用弹出窗口进行进一步的变换,但我无法在android webview中处理弹出窗口。
为了进行测试,我创建了两个简单的html文件来显示和关闭弹出窗口
window.html
<html>
<head>
<script language="Javascript">
function showFBWindow(){
url = "allowfbchild.html"
newwindow=window.open(url,'name','height=200,width=150');
if (window.focus) {newwindow.focus()}
}
</script>
</head>
<body>
<input type="button" OnClick="showFBWindow()" value="Open FB" />
</body>
</html>
allowfbchild.html
<html>
<head>
<script language="Javascript">
function redirectToFB(){
// window.opener.location.href="http://192.168.1.7/window.html";
self.close();
}
</script>
<h1>hello</h1>
</head>
<body>
<h1>hello</h1>
<input type="button" value="Ok" OnClick="redirectToFB()" />
<a href="www.google.com">google</a>
</body>
</html>
我在我的Android活动中尝试了这个:(如果有任何其他解决方法,建议这样做)
public class MainActivity extends Activity
{
RelativeLayout main;
// @Override
// public void onBackPressed()
// {
// if (webView.canGoBack())
// webView.goBack();
// else
// super.onBackPressed();
// }
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
try
{
setContentView(R.layout.activity_main);
main = (RelativeLayout) findViewById(R.id.main);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeclient());
main.addView(webView);
webView.loadUrl("http://192.168.1.7/window.html");
}
catch (Exception e)
{
e.printStackTrace();
}
}
class MyWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
System.err.println("load url" + url);
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
setProgressBarIndeterminateVisibility(false);
System.err.println("finish url" + url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, favicon);
setProgressBarIndeterminateVisibility(true);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
super.onReceivedError(view, errorCode, description, failingUrl);
System.err.println("error:" + description);
}
}
private class MyWebChromeclient extends WebChromeClient
{
@SuppressLint("SetJavaScriptEnabled")
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg)
{
System.err.println("create new");
WebView newWebView = new WebView(MainActivity.this);
newWebView.getSettings().setJavaScriptEnabled(true);
newWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
main.addView(newWebView);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return false;
}
});
return true;
}
@Override
public void onCloseWindow(WebView window)
{
super.onCloseWindow(window);
System.err.println("closing");
// main.removeViewAt(main.getChildCount() - 1);
}
}
}
请为此建议解决方案。感谢名单。