我想打开 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);
}
我有 SENDTO 意图过滤器,当 startActivity 调用时,我的应用程序出现在电子邮件客户端列表中。
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent-filter>
我删除了邮件
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
</intent-filter>
我的应用程序不再出现在邮件客户端列表中。
我没有对resolveActivity或startActivity进行任何更改。