我有一个用于发送电子邮件的代码
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();
}
但是当这个 Intent 被触发时,我会在列表中看到许多项目,例如 sms 应用、gmail 应用、facebook 应用等等。
我怎样才能过滤这个并只启用 gmail 应用程序(或者只是电子邮件应用程序)?
最佳答案
使用 android.content.Intent.ACTION_SENDTO
(new Intent(Intent.ACTION_SENDTO);
) 只获取邮件客户端列表,没有 facebook 或其他应用。只是电子邮件客户端。
我不建议您直接使用电子邮件应用程序。让用户选择他最喜欢的电子邮件应用程序。 不要约束他。
如果您使用 ACTION_SENDTO,putExtra 无法向 Intent 添加主题和文本。使用 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..."));
关于android - 通过 gmail 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8284706/