如何处理mailto:在android webview中

时间:2010-09-02 00:57:52

标签: android android-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()但它没有用。

这是我正在谈论的alt text

的一个例子

3 个答案:

答案 0 :(得分:6)

这是James Gray的答案的更强大版本。 它应该处理多个地址(以逗号分隔)和多个'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;
}

答案 1 :(得分:2)

这就是我所拥有的:

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

因为我看不到'之前'和之后......看起来它正在带走(或添加)链接上的粗体属性 - 检查{{1}的CSS(可能是JavaScript / Jquery)并查看它是否包含a:visitedfont-weight: normal;属性。

答案 2 :(得分:2)

这是一个更复杂的版本,不使用MailTo类(由于某种原因,它不能正确解析完整的mailto链接。只要它们存在,它就会连续拉动电子邮件,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:")) {
        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;
}