本文介绍了Android Web 视图 shouldOverrideUrlLoading() 已弃用.(替代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了一种使邮件在 android webview 中工作的方法,但该方法已被弃用.谁能给我新方法的完整代码片段.这是我在这个网站上找到的方法
Java 代码如下:
@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {如果(url.startsWith(电话:")){发起呼叫(网址);返回真;}if (url.startsWith("mailto:")) {sendEmail(url.substring(7));返回真;}返回假;}
但是当我的目标平台为 Android 7.1.1 时它不起作用
解决方案
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
所有Android版本都支持的那个有这个方法签名:
public boolean shouldOverrideUrlLoading(WebView view, String url)
我应该怎么做才能使其适用于所有版本?
你需要覆盖这两个方法
对于包括 Android N+
在内的每个 api,您都需要更改代码.
检查下面的代码.它将针对 N
和更高版本的 API
@SuppressWarnings("deprecation")@覆盖public boolean shouldOverrideUrlLoading(WebView view, String url) {如果(url.startsWith(电话:")){发起呼叫(网址);返回真;}if (url.startsWith("mailto:")) {sendEmail(url.substring(7));返回真;}返回假;}@RequiresApi(Build.VERSION_CODES.N)@覆盖public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {String url = request.getUrl().toString();如果(url.startsWith(电话:")){发起呼叫(网址);返回真;}if (url.startsWith("mailto:")) {sendEmail(url.substring(7));返回真;}返回假;}
I have Found a method to make mailto work in android webview but the method is deprecated.Can any one give me full code snippet of the new method.Here is the method I found on this site
Java code is below:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
initiateCall(url);
return true;
}
if (url.startsWith("mailto:")) {
sendEmail(url.substring(7));
return true;
}
return false;
}
But it's not working when I have target platform as Android 7.1.1
解决方案
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
public boolean shouldOverrideUrlLoading(WebView view, String url)
For every api including Android N+
you need to change your code.
Check this below code. It will target both lower API with N
and above
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
initiateCall(url);
return true;
}
if (url.startsWith("mailto:")) {
sendEmail(url.substring(7));
return true;
}
return false;
}
@RequiresApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.startsWith("tel:")) {
initiateCall(url);
return true;
}
if (url.startsWith("mailto:")) {
sendEmail(url.substring(7));
return true;
}
return false;
}
这篇关于Android Web 视图 shouldOverrideUrlLoading() 已弃用.(替代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!