如何处理mailto:在android webview中

如何处理mailto:在android webview中,android,android-webview,Android,Android Webview,我正在尝试截取我的应用程序中嵌入的webview中的mailto:链接。我所做的一切正常,但当用户按下链接时,它在返回应用程序时会变得模糊。下面是我在WebViewClient中所做的 @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.startsWith("mailto:")){ url = url.replaceFirst("mailto:"

我正在尝试截取我的应用程序中嵌入的webview中的mailto:链接。我所做的一切正常,但当用户按下链接时,它在返回应用程序时会变得模糊。下面是我在WebViewClient中所做的

    @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.startsWith("mailto:")){
        url = url.replaceFirst("mailto:", "");
        url = url.trim();
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url});
        context.startActivity(i);
        return true;
    }
    context.findViewById(R.id.loadingBar).setVisibility(View.VISIBLE);
    view.loadUrl(url);
    return true;
}
如果我执行view.reload()操作,确实可以解决问题,但是有没有更好的方法可以在不浪费带宽的情况下解决问题?我尝试了invalidate(),但没有成功

这是我所说的一个例子,这就是我得到的:

if (url.startsWith("mailto:")) {
    String[] blah_email = url.split(":");
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
    Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
    startActivity(emailIntent);
}

因为我看不到“之前”和“之后”。。。它似乎在删除(或添加)链接上的粗体属性-检查CSS(可能是JavaScript/Jquery)中的
a:visted
,看看它是否包含
font-weight:normal
字体大小:粗体
属性

这里有一个更复杂的版本,它不使用MailTo类(由于某些原因,它无法正确解析完整的mailto链接。只要它们存在,它就会连续提取电子邮件、抄送、密件抄送、主题和正文。如果它们不存在,它会跳过它们并转到下一个。但是,这要求链接创建者将所有内容按顺序排列,如果它不按顺序排列,它将无法工作。我可能稍后再制作一个这样的链接。)我不在乎它的顺序

对于那些关心此事的人来说,这也让市场应用程序的直接链接起到了作用

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url == null) { return false; }
    if (url.startsWith("market://")) {
        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        return true;
    }
    if (url.startsWith("mailto:")) {
        url = url.replaceFirst("mailto:", "");
        //
        String theEmail = "",
            theEmailCC = "",
            theEmailBCC = "",
            theSubject = "",
            theBody = "";
        Boolean hasEmail = true,
            hasEmailCC = url.contains("&cc="),
            hasEmailBCC = url.contains("&bcc="),
            hasSubject = url.contains("&subject="),
            hasBody = url.contains("&body=");
        int posEmail = 0,
            posEmailCC = hasEmailCC ? url.indexOf("&cc=") : 0,
            posEmailBCC = hasEmailBCC ? url.indexOf("&bcc=") : 0,
            posSubject = hasSubject ? url.indexOf("&subject=") : 0,
            posBody = hasBody ? url.indexOf("&body=") : 0;
        //
        if        (hasEmail    && hasEmailCC ) { theEmail    = url.substring(posEmail, posEmailCC - posEmail);
        } else if (hasEmail    && hasEmailBCC) { theEmail    = url.substring(posEmail, posEmailBCC - posEmail);
        } else if (hasEmail    && hasSubject ) { theEmail    = url.substring(posEmail, posSubject - posEmail);
        } else if (hasEmail    && hasBody    ) { theEmail    = url.substring(posEmail, posBody - posEmail);
        } else if (hasEmail                  ) { theEmail    = url;
        } else {                               /*theEmail    = url;*/ }

        if        (hasEmailCC  && hasEmailBCC) { theEmailCC  = url.substring(posEmailCC, posEmailBCC - posEmailCC);
        } else if (hasEmailCC  && hasSubject ) { theEmailCC  = url.substring(posEmailCC, posSubject - posEmailCC);
        } else if (hasEmailCC  && hasBody    ) { theEmailCC  = url.substring(posEmailCC, posBody - posEmailCC);
        } else if (hasEmailCC                ) { theEmailCC  = url.substring(posEmailCC);
        } else {                               /*theEmailCC  = url.substring(posEmailCC);*/ }
        theEmailCC = theEmailCC.replace("&cc=", "");

        if        (hasEmailBCC && hasSubject ) { theEmailBCC = url.substring(posEmailBCC, posSubject - posEmailBCC);
        } else if (hasEmailBCC && hasBody    ) { theEmailBCC = url.substring(posEmailBCC, posBody - posEmailBCC);
        } else if (hasEmailBCC               ) { theEmailBCC = url.substring(posEmailBCC);
        } else {                               /*theEmailBCC = url.substring(posEmailBCC);*/ }
        theEmailBCC = theEmailBCC.replace("&bcc=", "");

        if        (hasSubject  && hasBody    ) { theSubject  = url.substring(posSubject, posBody - posSubject);
        } else if (hasSubject                ) { theSubject  = url.substring(posSubject);
        } else {                               /*theSubject  = url.substring(posSubject);*/ }
        theSubject = theSubject.replace("&subject=", "");

        if        (hasBody                   ) { theBody     = url.substring(posBody);
        } else {                               /*theBody     = url.substring(posBody);*/ }
        theBody = theBody.replace("&body=", "");

        theSubject = theSubject.replace("%20", " ");
        theBody = theBody.replace("%20", " ").replace("%0A", "\n");
        //
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("message/rfc822");
        //
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { theEmail, });
        if (hasEmailCC) { emailIntent.putExtra(android.content.Intent.EXTRA_CC, theEmailCC); }
        if (hasEmailBCC) { emailIntent.putExtra(android.content.Intent.EXTRA_BCC, theEmailBCC); }
        if (hasSubject) { emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, theSubject); }
        if (hasBody) { emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, theBody); }
        //
        view.getContext().startActivity(emailIntent);
        //
        return true;
    }
    return false;
}

以下是詹姆斯·格雷答案的更有力版本。 它应该处理多个地址(逗号分隔)和多个“cc”/“bcc”参数:

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

  if (url == null) {
    return false;
  }
  if (url.startsWith("market://")) {
    view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    return true;
  }
  if (url.startsWith("mailto:")) {

    try {
      List<String> to = new ArrayList<String>();
      List<String> cc = new ArrayList<String>();
      List<String> bcc = new ArrayList<String>();
      String subject = null;
      String body = null;

      url = url.replaceFirst("mailto:", "");

      String[] urlSections = url.split("&");
      if (urlSections.length >= 2) {

        to.addAll(Arrays.asList(urlSections[0].split(",")));

        for (int i = 1; i < urlSections.length; i++) {
          String urlSection = urlSections[i];
          String[] keyValue = urlSection.split("=");

          if (keyValue.length == 2) {
            String key = keyValue[0];
            String value = keyValue[1];

            value = URLDecoder.decode(value, "UTF-8");

            if (key.equals("cc")) {
              cc.addAll(Arrays.asList(url.split(",")));
            }
            else if (key.equals("bcc")) {
              bcc.addAll(Arrays.asList(url.split(",")));
            }
            else if (key.equals("subject")) {
              subject = value;
            }
            else if (key.equals("body")) {
              body = value;
            }
          }
        }
      }
      else {
        to.addAll(Arrays.asList(url.split(",")));
      }

      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.setType("message/rfc822");

      String[] dummyStringArray = new String[0]; // For list to array conversion
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to.toArray(dummyStringArray));
      if (cc.size() > 0) {
        emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc.toArray(dummyStringArray));
      }
      if (bcc.size() > 0) {
        emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc.toArray(dummyStringArray));
      }
      if (subject != null) {
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
      }
      if (body != null) {
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
      }
      view.getContext().startActivity(emailIntent);

      return true;
    }
    catch (UnsupportedEncodingException e) {
      /* Won't happen*/
    }

  }
  return false;
}
@覆盖
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
如果(url==null){
返回false;
}
if(url.startsWith(“市场:/”){
view.getContext().startActivity(新的Intent(Intent.ACTION_视图,Uri.parse(url));
返回true;
}
if(url.startsWith(“mailto:”){
试一试{
List to=new ArrayList();
List cc=new ArrayList();
List bcc=new ArrayList();
字符串subject=null;
字符串体=null;
url=url.replaceFirst(“mailto:,”);
字符串[]urlSections=url.split(&);
如果(urlSections.length>=2){
to.addAll(Arrays.asList(urlSections[0].split(“,”));
for(int i=1;i0){
emailIntent.putExtra(android.content.Intent.EXTRA_CC,CC.toArray(dummyStringArray));
}
如果(bcc.size()>0){
emailIntent.putExtra(android.content.Intent.EXTRA_BCC,BCC.toArray(dummyStringArray));
}
if(主题!=null){
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,SUBJECT);
}
if(body!=null){
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,body);
}
view.getContext().startActivity(emailIntent);
返回true;
}
捕获(不支持的编码异常e){
/*不会发生的*/
}
}
返回false;
}

blued?你能上传一个例子吗?我甚至不知道android有一个模糊文本的工具。是的,我猜bluer是一个糟糕的措辞。谢谢。问题不是试图发送电子邮件,而是当你点击链接时文本变得模糊(见上图)当他们从电子邮件客户端返回应用程序时。这真的很奇怪-你使用的是什么版本的android?我认为这是一个特定于页面的问题?我会检查那里是否只有1个-因为当你点击链接时,它会改变css样式-看起来像是
$('#elm_id).css('font-weight','normal');
(删除(或添加)粗体属性)您也可以使用setType(“应用程序/八位字节流”)或setType(“消息/rfc822”)代替setType(“文本/普通”)。所有这些都不能将其仅限于电子邮件应用,但message/rfc822似乎是最接近的。您可以通过以下方式改进您的解决方案:您可以使用
Intent-emailIntent=new-Intent(Intent.ACTION\u-SENDTO,Uri.parse(url));
,而不必再使用
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,新字符串[]{blah_EMAIL[1]})
这肯定比我当时写的要好,而且更接近我现在写的内容,因为我对编程有了更多的知识。谢谢你花我这么多时间。我也会将行改为在“?”上拆分邮件。例如-->String[]urlSections=url.split(\\?|&)似乎是一个很好的解决方案,但它不应该是for循环中的value.split(,)而不是url.split(,)?