微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何处理mailto:在android webview中

我试图在我的应用程序中拦截嵌入式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()但它没有用.

这是我正在谈论的一个例子

解决方法:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐