我有一个火灾意图发送电子邮件的代码
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, msg);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Start.this,
"There are no email clients installed.",
Toast.LENGTH_SHORT).show();
}
但是当这个意图被解雇时,我在列表中看到很多项目,如短信应用,gmail应用,脸谱app等等。
如何过滤此功能并仅启用gmail应用(或者只是电子邮件应用)?
答案 0 :(得分:80)
使用android.content.Intent.ACTION_SENDTO
(new Intent(Intent.ACTION_SENDTO);
)仅获取电子邮件客户端列表,不包含Facebook或其他应用。只是电子邮件客户端。
我不建议您直接访问电子邮件应用。让用户选择他最喜欢的电子邮件应用。 不要限制他。
如果您使用ACTION_SENDTO,则putExtra无法向主题添加主题和文本。使用Uri添加主题和正文。
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode("email@gmail.com") +
"?subject=" + Uri.encode("the subject") +
"&body=" + Uri.encode("the body of the message");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "Send mail..."));
答案 1 :(得分:21)
接受的答案对4.1.2无效。这应该适用于所有平台:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
希望这会有所帮助。
答案 2 :(得分:13)
Igor Popov的回答是100%正确,但如果你想要一个后备选项,我会使用这种方法:
public static Intent createEmailIntent(final String toEmail,
final String subject,
final String message)
{
Intent sendTo = new Intent(Intent.ACTION_SENDTO);
String uriText = "mailto:" + Uri.encode(toEmail) +
"?subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(message);
Uri uri = Uri.parse(uriText);
sendTo.setData(uri);
List<ResolveInfo> resolveInfos =
getPackageManager().queryIntentActivities(sendTo, 0);
// Emulators may not like this check...
if (!resolveInfos.isEmpty())
{
return sendTo;
}
// Nothing resolves send to, so fallback to send...
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
send.putExtra(Intent.EXTRA_EMAIL,
new String[] { toEmail });
send.putExtra(Intent.EXTRA_SUBJECT, subject);
send.putExtra(Intent.EXTRA_TEXT, message);
return Intent.createChooser(send, "Your Title Here");
}
答案 3 :(得分:7)
这是从Android官方文档中引用的,我在Android 4.4上测试过,并且运行得很好。请参阅https://developer.android.com/guide/components/intents-common.html#Email
上的更多示例 public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
答案 4 :(得分:6)
替换
i.setType("text/plain");
与
// need this to prompts email client only
i.setType("message/rfc822");
答案 5 :(得分:-1)
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","opinions@gmail.com.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "IndiaTV News - Mobile App Feedback");
emailIntent.putExtra(Intent.EXTRA_TEXT,Html.fromHtml(Settings.this.getString(R.string.MailContent)));
startActivityForResult(Intent.createChooser(emailIntent, "Send email..."),0);