问题描述
我想用预格式化的电子邮件打开 Gmail。 我正在使用此代码:
public static void sendEmail(Context context,String receiverAddress,String title,String body) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { receiverAddress });
emailIntent.putExtra(Intent.EXTRA_SUBJECT,title);
if (body != null) {
emailIntent.putExtra(Intent.EXTRA_TEXT,body);
}
if (emailIntent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(emailIntent);
}
}
但是,只有当我将此 intent-filter
添加到我的应用程序的清单文件时它才有效:
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
通过这样做,它会向我显示一个包含两个应用的应用选择器:我的应用和Gmail。
但是我不希望我的应用成为这个意图的接收者。我只希望 Gmail(和其他电子邮件客户端)收到此意图。
但是,如果我不添加 intent-filter
,则什么也不会发生。
我做错了什么?
解决方法
您可以尝试以下方法吗?这就是我使用的。据我所知,您的代码很好,选择器的事情不应该影响我的感觉,但我仍然建议尝试以下一次。我觉得可能是选择器导致了问题。
public void composeEmail(String[] addresses,String subject) {
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("mailto:")); // only email apps should handle this
sendIntent.putExtra(Intent.EXTRA_EMAIL,addresses);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,subject);
Intent shareIntent = Intent.createChooser(sendIntent,null);
startActivity(shareIntent);
}