我试图在我的应用程序中拦截嵌入式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 { Listto = new ArrayList (); List cc = new ArrayList (); List bcc = new ArrayList (); 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; }