Android 7.0或更高版本的mailto链接无法在webView中运行

时间:2017-03-13 07:01:57

标签: android android-intent android-webview android-7.0-nougat

我的代码是:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    ButterKnife.bind(this);

    String url = getIntent().getStringExtra(EXTRA_URL);

    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(url);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    });
}

按下发送邮件链接时,显示以下错误:

净:: ERR_UNKNOWN_URL_SCHEME

问候!!

1 个答案:

答案 0 :(得分:0)

在设置webviewclient之前加载网址。尝试先设置webviewclient,然后再调用loadUrl()。

修改

@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {

    Uri uri = Uri.parseUri(url);
    if ("mailto".equalsIgnoreCase(uri.getScheme()) == true) {
        try {

            // handle mailto-urls...
            Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
            sendIntent.setData(uri);
            view.getContext().startActivity(sendIntent);        

        } catch (ActivityNotFoundException e) {}
        return true;
    }
    // load everything else in webview
    return false;
}