简体   繁体   中英

Android Webview ERR_UNKNOWN_URL_SCHEME Error

When I click a link which goes to mailto:admin@ikiyuzoniki.net I get this error:

net: ERR_UNKNOWN_URL_SCHEME

I tried to add an if(url.startsWith("mailto:")) condition but it's not working.

This is my MyWebViewClient method:

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;
    }

and this is how I add the function to my web view before loadUrl :

...
mWebview.setWebViewClient(new MyWebViewClient());
...

try this

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;
    } 

This error is appeared because the WebView can't recognize the URL Scheme,for example, the WebView will usually recognize http and https, anything other than these, for example – intent://,market://,app://,mail:// etc will not be recognized by webview unless we add a handler to handle these url schemes or by disabling these schemes and only load http and https schemes.

Here is an example to fix the common intent url scheme.

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;
}

his is my MyWebViewClient method:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM