如何使用预先填写的“收件人”字段启动电子邮件应用程序?

时间:2009-11-20 19:21:35

标签: android email android-activity android-intent

我尝试了这个代码,我在这里找到了:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "testemail@gmail.com", null)); startActivity(intent);

但是我在屏幕上看到一条消息,上面写着“不支持的动作”。关于如何使这个工作的任何想法?

谢谢!

3 个答案:

答案 0 :(得分:28)

试试这个snippet

/* Create the Intent */
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

/* Fill it with Data */
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"to@email.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");

/* Send it off to the Activity-Chooser */
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));

关键部分:使用EXTRA_EMAIL表示您的地址,并使用createChooser()以防用户配置多个电子邮件客户端。

答案 1 :(得分:4)

你试过吗

Intent intent = new Intent(
    Intent.ACTION_SENDTO,
    Uri.parse("mailto:testemail@gmail.com")
);
startActivity(intent);

答案 2 :(得分:1)

我认为这里真正的问题是你在官方模拟器上运行,你的意图不匹配。

从我的测试来看,这是一个问题,当意图的URI(来自setData())与任何东西都不匹配并且您正在其中一个官方Android模拟器上运行时会发生这种情况。这似乎不会发生在真实设备上,所以它不应该是一个现实世界的问题。

您可以使用此代码在启动意图之前检测何时发生这种情况:

ComponentName emailApp = intent.resolveActivity(getPackageManager());
ComponentName unsupportedAction = ComponentName.unflattenFromString("com.android.fallback/.Fallback");
boolean hasEmailApp = emailApp != null && !emailApp.equals(unsupportedAction);

(显示“不支持的操作”操作方法的活动的名称为com.android.fallback.FallbackActivity。)