這是一個更復雜的版本,它不使用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;
}
模糊?你可以上傳一個例子嗎?我甚至不知道android有一個模糊文本的實用工具。 – QRohlf 2010-09-02 05:42:19
是的,我猜模糊是不好的措辭。謝謝。 – schwiz 2010-09-02 06:55:19