真的应该弃用`shouldOverrideUrlLoading`吗?我可以用什么代替呢? [英] Is `shouldOverrideUrlLoading` really deprecated? What can I use instead?
问题描述
"shouldOverrideUrlLoading"是否已被弃用?如果是这样,我该怎么用呢?
Is "shouldOverrideUrlLoading" really deprecated? If so, what can I use instead?
shouldOverrideUrlLoading
似乎已弃用定位到Android N ,并且我需要使应用程序自API 19起一直运行到最新的Android N(测试版),我使用的某些功能是Android N的新功能(例如Data Saver),因此针对棉花糖将无济于事,因为我需要使用这些新功能,这是我使用的代码的一部分:
It seems like shouldOverrideUrlLoading
is deprecated targeting Android N and I need to make an app work since API 19 until the latest right now which is Android N (beta), I use some features that are new in Android N (like Data Saver), so targeting Marshmallow will not help with the issue since I need to use those new features, here is the part of the code I use:
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
...
} else if (url.startsWith("sms:")) {
...
}
...
}
这是Android Studio给我的消息:
And this is the message Android Studio gave me:
覆盖"android.webkit.WebViewClient"中不推荐使用的方法 该检查报告在指定的检查范围中使用了不赞成使用的代码.
Overrides deprecated method in 'android.webkit.WebViewClient' This inspection reports where deprecated code is used in the specified inspection scope.
I wonder if using @SuppressWarnings("deprecation")
will let me work on all devices since the API 19 until the latest Android N Beta (and its final version when it gets released), I can't test it myself, I never used that and I need to be sure that it works, so, anyone can tell?
推荐答案
我认为我使用的版本是一个很好的版本,因为它与Android Developer Docs完全相同,除了字符串的名称,他们使用的是"view",我使用的是"webview",其余的一样
The version I'm using I think is the good one, since is the exact same as the Android Developer Docs, except for the name of the string, they used "view" and I used "webview", for the rest is the same
不,不是.
N Developer Preview的新功能具有以下方法签名:
The one that is new to the N Developer Preview has this method signature:
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
所有Android版本(包括N)都支持的方法具有以下方法签名:
The one that is supported by all Android versions, including N, has this method signature:
public boolean shouldOverrideUrlLoading(WebView view, String url)
那我为什么要使它在所有版本上都能工作?
So why should I do to make it work on all versions?
覆盖已弃用的参数,该参数以String
作为第二个参数.
Override the deprecated one, the one that takes a String
as the second parameter.
这篇关于真的应该弃用`shouldOverrideUrlLoading`吗?我可以用什么代替呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!