Luk*_*kap 26 email gmail android android-intent
我有一个火灾意图发送电子邮件的代码
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();
}
Run Code Online (Sandbox Code Playgroud)
但是当这个意图被解雇时,我在列表中看到很多项目,如sms app,gmail app,facebook app等等.
如何过滤此功能并仅启用gmail应用(或者只是电子邮件应用)?
Igo*_*pov 81
使用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..."));
Run Code Online (Sandbox Code Playgroud)
tha*_*h84 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..."));
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
xba*_*esx 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");
}
Run Code Online (Sandbox Code Playgroud)
这是从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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
更换
i.setType("text/plain");
Run Code Online (Sandbox Code Playgroud)
同
// need this to prompts email client only
i.setType("message/rfc822");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33418 次 |
| 最近记录: |