我在 WebView 中添加了一个移动谷歌广告(在一个 fragment 中)。当您点击我想在新浏览器中打开的广告时。我启用了以下功能:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
google.js 正在生成一个 iFrame,但是点击 webview 似乎没有注册为新的链接点击,也没有调用 shouldOverrideUrlLoading
最佳答案
在另一个so问题中找到答案here ,
但是我修改了它以适用于 fragment (包括 getActivity())。 请注意,这是专门针对谷歌广告的。在您的 webclient 中覆盖以下方法。
@Override
public void onLoadResource(WebView view, String url)
{
// the url that contains the ad to link to contains googleads, this is risky if google
// change it, but there couldn't find a nice way around
if (url.contains("googleads"))
{
HitTestResult hitTestResult = view.getHitTestResult();
// Check is the hit test is a src tag with image anchor (i.e an mobile ad)
if (hitTestResult.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)
{
getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
}
}
}
关于javascript - android webview 从 Javascript 打开浏览器窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9115009/