Android 的 WebView 的默认行为是否更改为在内部打开所有链接?
Is default behavior of Android's WebView changed to open internally all links?
我注意到随着 Google System WebView 的最后一次更新,我的 WebView 中的所有链接都在视图本身中打开。但是根据 google 的文档:
public boolean shouldOverrideUrlLoading (WebView view, String url)
Added in API level 1
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
我没有提供自定义 WebViewClient。
注意:我注意到该问题的设备是 HTC One,其最新的 Google System WebView 来自 2015 年 6 月 8 日
我可以重现这些发现。 Android 系统 WebView 43.0.2357.121 表现出您描述的行为,而我在升级到它之前使用的版本没有。
我有 filed a bug report on this,现在需要做更多的测试并警告世界。
感谢您指出这一点!
更新
Here is a blog post 我就这个主题写的。引用我自己的话:
My recommendation at the moment is:
Always attach a WebViewClient
to your WebView
Always implement shouldOverrideUrlLoading()
on the WebViewClient
Always return true
to indicate that you are handling the event
Always do what your app needs to have done, whether that is loading
the URL into the WebView
or launching a browser on the URL (rather
than returning false
and relying on stock behavior)
Something like this static inner class appears to do the trick —
create an instance and pass it to setWebViewClient()
on your
WebView
:
private static class URLHandler extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (shouldKeepInWebView(url)) {
view.loadUrl(url);
}
else {
Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(i);
}
return(true);
}
private boolean shouldKeepInWebView(String url) {
return(true); // or false, or use regex, or whatever
}
}
(where you would put your business logic in shouldKeepInWebView()
to
determine whether or not a given URL should stay in the WebView
or launch a browser)
在我看来,这个问题已在 44.0.240.54 中解决。
我注意到随着 Google System WebView 的最后一次更新,我的 WebView 中的所有链接都在视图本身中打开。但是根据 google 的文档:
public boolean shouldOverrideUrlLoading (WebView view, String url) Added in API level 1
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
我没有提供自定义 WebViewClient。
注意:我注意到该问题的设备是 HTC One,其最新的 Google System WebView 来自 2015 年 6 月 8 日
我可以重现这些发现。 Android 系统 WebView 43.0.2357.121 表现出您描述的行为,而我在升级到它之前使用的版本没有。
我有 filed a bug report on this,现在需要做更多的测试并警告世界。
感谢您指出这一点!
更新
Here is a blog post 我就这个主题写的。引用我自己的话:
My recommendation at the moment is:
Always attach a
WebViewClient
to yourWebView
Always implement
shouldOverrideUrlLoading()
on theWebViewClient
Always return
true
to indicate that you are handling the eventAlways do what your app needs to have done, whether that is loading the URL into the
WebView
or launching a browser on the URL (rather than returningfalse
and relying on stock behavior)Something like this static inner class appears to do the trick — create an instance and pass it to
setWebViewClient()
on yourWebView
:
private static class URLHandler extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (shouldKeepInWebView(url)) {
view.loadUrl(url);
}
else {
Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(i);
}
return(true);
}
private boolean shouldKeepInWebView(String url) {
return(true); // or false, or use regex, or whatever
}
}
(where you would put your business logic in
shouldKeepInWebView()
to determine whether or not a given URL should stay in theWebView
or launch a browser)
在我看来,这个问题已在 44.0.240.54 中解决。