handling links in a webview

后端 未结 3 2101
滥情空心
滥情空心 2020-11-30 00:31

I have my WebView loading all links inside the webview - but when I select an email link it tries to load it in the webview instead of launching an email app on

3条回答
  •  無奈伤痛
    2020-11-30 01:24

    I assume you are already overriding shouldOverrideUrlLoading, you just need to handle this special case.

    mWebClient = new WebViewClient(){
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.startsWith("mailto:")){
                    MailTo mt = MailTo.parse(url);
                    Intent i = newEmailIntent(MyActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
                    startActivity(i);
                    view.reload();
                    return true;
                }
    
                    else{
                        view.loadUrl(url);
                    }
                    return true;
                }
           };
        mWebView.setWebViewClient(mWebClient);
    
        public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
          Intent intent = new Intent(Intent.ACTION_SEND);
          intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
          intent.putExtra(Intent.EXTRA_TEXT, body);
          intent.putExtra(Intent.EXTRA_SUBJECT, subject);
          intent.putExtra(Intent.EXTRA_CC, cc);
          intent.setType("message/rfc822");
          return intent;
    }
    

提交回复
热议问题