问题描述
在API21中,Google修改了shouldInterceptRequest方法以使用WebResourceRequest request
而不是String url
.有什么办法可以编写扩展WebViewClient
的通用类并处理这两种方法?我的最低API版本是18.
In API21 Google modified shouldInterceptRequest method to use WebResourceRequest request
instead of String url
.Is there any way I could write a generic class extending WebViewClient
and handle both methods?My minimum API version is 18.
谢谢克里斯汀(Krystian)
ThanksKrystian
推荐答案
否,他们添加了 second shouldInterceptRequest()
方法.两者都可以在API Level 21+中获得; String
变体在API Level 11+上可用.虽然String
被标记为已弃用,但String
变体应在相当长的时间内得到支持,以实现向后兼容.
No, they added a second shouldInterceptRequest()
method. Both are available in API Level 21+; the String
variant is available on API Level 11+. While the String
one is marked as deprecated, the String
variant should be supported for quite some time, for backwards compatibility.
shouldInterceptRequest()
版本的WebResourceRequest
版本的内置实现仅调用shouldInterceptRequest()
的String
实现:
The built-in implementation of the WebResourceRequest
version of shouldInterceptRequest()
simply calls the String
implementation of shouldInterceptRequest()
:
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return shouldInterceptRequest(view, request.getUrl().toString());
}
(来自源代码截至目前)
因此,您有两种选择:
-
只需覆盖
String
版本,如果不需要WebResourceRequest
,它将在所有相关的API级别上使用.
Just override the
String
edition, if you do not need theWebResourceRequest
, and it will be used on all relevant API levels.
同时覆盖这两个方面,知道WebResourceRequest
版本将在API级别21+上使用,而String
版本将在API级别11-20上使用.
Override both, knowing that the WebResourceRequest
one will be used on API Level 21+ and the String
edition will be used on API Levels 11-20.
这篇关于如何处理API21中的shouldInterceptRequest参数更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!