Android (ICS):在修改 window.location 的 HTML/JS 中使用 loadData 时,WebView 无法显示
在传递特定 HTML 有效负载时,我在使用 WebView 的 loadData
方法时遇到问题。有效负载包含一些 JavaScript,用于将 window.location
设置为具有自定义方案的 URL(以下示例中的 mycustomscheme://
)。原因是我希望我的应用程序代码能够在 shouldOverrideUrlLoading
的实现中捕获各种 JS 事件。
在我的 Galaxy Nexus 设备(运行 Ice Cream Sandwich)上,shouldOverrideUrlLoading
按预期工作,但 WebView 从未出现在屏幕上。如果我从 JS 中删除 window.location
代码,WebView 就会正常显示。该代码似乎也可以在我尝试过的所有 ICS 之前的设备上正常工作。
我在下面的代码中遗漏了一些明显的东西吗?
编辑: 我可以使用 addJavascriptInterface
来完成同样的事情,但我不想这样做,因为并非所有 HTML 都是由我生成的。为了保持代码示例的清晰性,我省略了这个细节。
public class ICSWebViewTestActivity extends Activity {
private RelativeLayout mLayout;
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new RelativeLayout(this);
mLayout.setBackgroundColor(Color.RED);
setContentView(mLayout);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setBackgroundColor(Color.GREEN);
mWebView.setWebViewClient(new MyWebViewClient());
String data =
"<html><head>" +
"<script>" +
"window.onload = f;" + // works if this line is removed
"function f() { window.location = 'mycustomscheme://finish'; }" +
"</script>" +
"</head>" +
"<body><h1 style='color:blue'>Hello, world!</h1></body>";
mWebView.loadData(data, "text/html", "utf-8");
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
mLayout.addView(mWebView, rlp);
}
private void doAction() {
Toast.makeText(this, "Doing action!", Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("mycustomscheme://")) {
doAction();
}
return true;
}
}
}
I'm experiencing trouble with WebView's loadData
method when passing in a certain HTML payload. The payload contains some JavaScript that sets window.location
to a URL with a custom scheme (mycustomscheme://
in the example below). The reason for this is that I'd like my application code to be able to catch various JS events in its implementation of shouldOverrideUrlLoading
.
On my Galaxy Nexus device (running Ice Cream Sandwich), shouldOverrideUrlLoading
works as intended, but the WebView doesn't ever appear on-screen. If I remove the window.location
code from the JS, the WebView shows up just fine. The code also seems to work correctly on all pre-ICS devices I've tried.
Am I missing something obvious in the code below?
Edit: I could use addJavascriptInterface
to accomplish the same thing, but I'd prefer not to do that, since not all of the HTML is generated by me. To preserve the clarity of the code sample, I've omitted this detail.
public class ICSWebViewTestActivity extends Activity {
private RelativeLayout mLayout;
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new RelativeLayout(this);
mLayout.setBackgroundColor(Color.RED);
setContentView(mLayout);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setBackgroundColor(Color.GREEN);
mWebView.setWebViewClient(new MyWebViewClient());
String data =
"<html><head>" +
"<script>" +
"window.onload = f;" + // works if this line is removed
"function f() { window.location = 'mycustomscheme://finish'; }" +
"</script>" +
"</head>" +
"<body><h1 style='color:blue'>Hello, world!</h1></body>";
mWebView.loadData(data, "text/html", "utf-8");
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT);
mLayout.addView(mWebView, rlp);
}
private void doAction() {
Toast.makeText(this, "Doing action!", Toast.LENGTH_SHORT).show();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("mycustomscheme://")) {
doAction();
}
return true;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其设置
window.location
,为什么不调用 addJavascriptInterface 到WebView,然后从javascript调用interfaceName?Instead of setting
window.location
, why not call addJavascriptInterface to the WebView and then call the interfaceName from javascript?