我使用以下代码就可以了。所有的http网址都在webview中打开,“tel:”链接在拨号器中打开,“mailto:”链接在电子邮件客户端中打开。
但我的问题是如何将“mailto:”链接的主题更改为不同于预定义主题的内容。我猜测应该有2个单独的意图,1个用于“tel:”链接& 1为“mailto:”链接。我根本不知道如何将代码放入下面的shouldOverrideUrlLoading方法中。或者我可能正在使用错误的方法来满足我的需求。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
return true;
}
我设法让我自己的主题“mailto:”与Intent一起工作,但代码中没有包含“tel:”链接。那么如何才能在“mailto:”链接中使用我自己的主题?
非常感谢任何想法或建议!
答案 0 :(得分:8)
这是我的解决方案&这个对我有用。我希望它能帮助其他人解决我遇到的同样问题。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
}
else if (url.startsWith("mailto:")) {
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("application/octet-stream");
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{"email address"});
mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
}
return true;
}
答案 1 :(得分:1)
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith(TEL_PREFIX)) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
if (url.startsWith("mailto:")) {
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"a.ee.ee@rt.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
email.putExtra(Intent.EXTRA_TEXT, body);
//need this to prompts email client only
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
return true;
}
return false;
}
});
答案 2 :(得分:0)
当您检测到mailto时:尝试向其添加“?subject = custom”。我猜是这样的事情:
if (url.startsWith("mailto:")) {
Uri mailWithSubject = Uri.parse(url).
buildUpon().appendQueryParameter("subject", "Hello World!").build();
Intent intent = new Intent(Intent.ACTION_VIEW, mailWithSubject);
...
}
答案 3 :(得分:0)
为所有mailto链接收件人填写MainActivity
。
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
}
else if (url.startsWith("mailto:")) {
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent mail = new Intent(Intent.ACTION_SEND);
Intent intent = mail.setType("application/octet-stream");
// use for any mailto link for the recipient
MailTo recipient = MailTo.parse(url);
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient.getTo()});
mail.putExtra(Intent.EXTRA_SUBJECT, "Subject");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
}
return true;
}