Android 网络视图 ERR_UNKNOWN_URL_SCHEME 错误

Android Webview ERR_UNKNOWN_URL_SCHEME Error

当我单击转到 mailto:admin@ikiyuzoniki.net 的 link 时,出现此错误:

net: ERR_UNKNOWN_URL_SCHEME

我试图添加一个 if(url.startsWith("mailto:")) 条件,但它不起作用。

这是我的MyWebViewClient方法:

public class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.setVisibility(View.VISIBLE);
            final Animation fade = new AlphaAnimation(0.0f, 1.0f);
            fade.setDuration(200);
            view.startAnimation(fade);
            view.setVisibility(View.VISIBLE);

        }

    }
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.startsWith("mailto:")){
            Intent intent = null;
            try {
                intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            view.getContext().startActivity(intent);
        }
        else if (url.endsWith(".mp3")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "audio/*");
            startActivity(intent);

        } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(url), "video/*");
            startActivity(intent);
        }
        else {
            return false;
        }
        view.reload();
        return true;
    }

这就是我在 loadUrl:

之前将函数添加到我的 Web 视图的方式
...
mWebview.setWebViewClient(new MyWebViewClient());
...

试试这个

if(url.startsWith("mailto:")){
        MailTo mt = MailTo.parse(url);
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{mt.getTo()});
        i.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject());
        i.putExtra(Intent.EXTRA_CC, mt.getCc());
        i.putExtra(Intent.EXTRA_TEXT, mt.getBody());
        mContext.startActivity(i);
        view.reload();
        return true;
    } 

出现这个错误是因为WebView无法识别URLScheme,例如WebView通常会识别http和https,除此之外的任何东西,例如-intent://,market ://,app://,mail:// 等不会被 webview 识别,除非我们添加处理程序来处理这些 url 方案或禁用这些方案并仅加载 http 和 https 方案。

这里是修复通用意图 url 方案的示例。

mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return loadUrl(view, url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}

@Override
public void onReceivedError(WebView view, int errorCode, String description,String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//Log.d("Web Request Error", "");
showError(errorCode);
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.d("WebResource error", "");
int errorCode = error.getErrorCode();
showError(errorCode);
}
});

private boolean loadUrl(WebView view, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
} else if (url.toLowerCase().startsWith("mailto:")) {
MailTo mt = MailTo.parse(url);
Intent emailIntent = newEmailIntent(mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
startActivity(emailIntent);
return true;
}
return true;
}

这是我的 MyWebViewClient 方法:

public class MyWebViewClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        view.setVisibility(View.VISIBLE);
        final Animation fade = new AlphaAnimation(0.0f, 1.0f);
        fade.setDuration(200);
        view.startAnimation(fade);
        view.setVisibility(View.VISIBLE);

    }

}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.startsWith("mailto:")){
        Intent intent = null;
        try {
            intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        view.getContext().startActivity(intent);
    }
    else if (url.endsWith(".mp3")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "audio/*");
        startActivity(intent);

    } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse(url), "video/*");
        startActivity(intent);
    }
    else {
        return false;
    }
    view.reload();
    return true;
}