I have a question/problem, is "shouldOverrideUrlLoading" really deprecated? If so, what can I use instead?
我有一个问题/问题,“shouldOverrideUrlLoading”真的不赞成吗?如果有,我可以用什么代替?
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:
似乎shouldOverrideUrlLoading弃用针对安卓N和我需要一个应用程序API 19日以来工作直到现在最新的Android N(β),我使用一些功能的新Android N(如数据保护),所以针对棉花糖不能解决这个问题,因为我需要使用这些新特性,这是我使用的部分代码:
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
...
} else if (url.startsWith("sms:")) {
...
}
...
}
And this is the message Android Studio gave me:
这是Android Studio给我的消息:
Overrides deprecated method in 'android.webkit.WebViewClient' This inspection reports where deprecated code is used in the specified inspection scope.
重写“android.webkit”中不使用的方法。WebViewClient的这个检查报告在指定的检查范围中使用了废弃的代码。
Google says nothing about that deprecation.
谷歌并没有提到这种贬低。
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?
我想知道使用@SuppressWarnings(“弃用”)将让我工作以来所有设备API 19直到最新的Android Nβ(及其最终版本发布的时候),我自己不能测试它,我从未使用过,我需要确保它的工作原理,所以,有人可以告诉吗?
Thanks a lot.
非常感谢。
3 个解决方案
#1
69
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
我使用的版本我认为是好的,因为它和Android开发者文档是完全一样的,除了字符串的名字,他们用的是“view”,我用的是“webview”,其余的也是一样的
No, it is not.
不,它不是。
The one that is new to the N Developer Preview has this method signature:
N开发者预览版的新版本有这个方法签名:
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
The one that is supported by all Android versions, including N, has this method signature:
包括N在内的所有Android版本都支持这个方法签名:
public boolean shouldOverrideUrlLoading(WebView view, String url)
So why should I do to make it work on all versions?
那么我为什么要让它在所有版本上运行呢?
Override the deprecated one, the one that takes a String
as the second parameter.
覆盖已弃用的那个,它接受一个字符串作为第二个参数。
#2
138
Documenting in detail for future readers:
为将来的读者详细记录:
The short answer is you need to override both the methods. The shouldOverrideUrlLoading(WebView view, String url)
method is deprecated in API 24 and the shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
method is added in API 24. If you are targeting older versions of android, you need the former method, and if you are targeting 24 (or later, if someone is reading this in distant future) it's advisable to override the latter method as well.
简而言之,您需要重写这两个方法。在API 24中不赞成使用shouldOverrideUrlLoading(WebView, String url)方法,而在API 24中添加了shouldOverrideUrlLoading(WebView, WebResourceRequest请求)方法。如果你的目标是旧版本的android,你需要前一种方法,如果你的目标是24(或者更晚,如果有人在遥远的将来读到这个),最好也重写后一种方法。
The below is the skeleton on how you would accomplish this:
以下是如何实现这一目标的基本框架:
class CustomWebViewClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(uri);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
private boolean handleUri(final Uri uri) {
Log.i(TAG, "Uri =" + uri);
final String host = uri.getHost();
final String scheme = uri.getScheme();
// Based on some condition you need to determine if you are going to load the url
// in your web view itself or in a browser.
// You can use `host` or `scheme` or any part of the `uri` to decide.
if (/* any condition */) {
// Returning false means that you are going to load this url in the webView itself
return false;
} else {
// Returning true means that you need to handle what to do with the url
// e.g. open web page in a Browser
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
}
}
Just like shouldOverrideUrlLoading
, you can come up with a similar approach for shouldInterceptRequest
method.
就像shouldOverrideUrlLoading一样,您也可以为shouldInterceptRequest方法提供类似的方法。
#3
13
Use
使用
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
#1
69
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
我使用的版本我认为是好的,因为它和Android开发者文档是完全一样的,除了字符串的名字,他们用的是“view”,我用的是“webview”,其余的也是一样的
No, it is not.
不,它不是。
The one that is new to the N Developer Preview has this method signature:
N开发者预览版的新版本有这个方法签名:
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
The one that is supported by all Android versions, including N, has this method signature:
包括N在内的所有Android版本都支持这个方法签名:
public boolean shouldOverrideUrlLoading(WebView view, String url)
So why should I do to make it work on all versions?
那么我为什么要让它在所有版本上运行呢?
Override the deprecated one, the one that takes a String
as the second parameter.
覆盖已弃用的那个,它接受一个字符串作为第二个参数。
#2
138
Documenting in detail for future readers:
为将来的读者详细记录:
The short answer is you need to override both the methods. The shouldOverrideUrlLoading(WebView view, String url)
method is deprecated in API 24 and the shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
method is added in API 24. If you are targeting older versions of android, you need the former method, and if you are targeting 24 (or later, if someone is reading this in distant future) it's advisable to override the latter method as well.
简而言之,您需要重写这两个方法。在API 24中不赞成使用shouldOverrideUrlLoading(WebView, String url)方法,而在API 24中添加了shouldOverrideUrlLoading(WebView, WebResourceRequest请求)方法。如果你的目标是旧版本的android,你需要前一种方法,如果你的目标是24(或者更晚,如果有人在遥远的将来读到这个),最好也重写后一种方法。
The below is the skeleton on how you would accomplish this:
以下是如何实现这一目标的基本框架:
class CustomWebViewClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(uri);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
private boolean handleUri(final Uri uri) {
Log.i(TAG, "Uri =" + uri);
final String host = uri.getHost();
final String scheme = uri.getScheme();
// Based on some condition you need to determine if you are going to load the url
// in your web view itself or in a browser.
// You can use `host` or `scheme` or any part of the `uri` to decide.
if (/* any condition */) {
// Returning false means that you are going to load this url in the webView itself
return false;
} else {
// Returning true means that you need to handle what to do with the url
// e.g. open web page in a Browser
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
}
}
Just like shouldOverrideUrlLoading
, you can come up with a similar approach for shouldInterceptRequest
method.
就像shouldOverrideUrlLoading一样,您也可以为shouldInterceptRequest方法提供类似的方法。
#3
13
Use
使用
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}