Android Webview ERR_UNKNOWN_URL_SCHEME错误

时间:2015-04-24 16:49:30

标签: android mailto

当我点击发送到mailto:admin@ikiyuzoniki.net的链接时,我收到此错误:

  

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之前将该功能添加到我的网页视图中的方法:

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

3 个答案:

答案 0 :(得分:7)

试试这个

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

答案 1 :(得分:2)

出现此错误是因为WebView无法识别URL方案,例如,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;
}

答案 2 :(得分:1)

err_unknown_url_scheme android webview problem solved-这对我有用...他们的HTML网页还处理tel,mailto,sms和geo。

webview.setWebViewClient(new MyWebViewClient());

   webview.loadUrl("file:///android_asset/demo.html");

    }


    class MyWebViewClient extends WebViewClient
    {

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

            if( URLUtil.isNetworkUrl(url) )
            {
                return false;
            }
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }catch(ActivityNotFoundException e)
            {
                Log.e("AndroiRide",e.toString());
                Toast.makeText(MainActivity.this,"No activity found",Toast.LENGTH_LONG).show();
            }

            return true;
        }

        @RequiresApi(Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
        {
            String url=request.getUrl().toString();
            if( URLUtil.isNetworkUrl(url) )
            {
                return false;
            }
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }catch(ActivityNotFoundException e)
            {
                Log.e("AndroiRide",e.toString());
                Toast.makeText(MainActivity.this,"No activity found",Toast.LENGTH_LONG).show();
            }

            return true;
        }
    }

}`