Android:除非添加意图过滤器,否则电子邮件意图 ACTION_SENDTO 不起作用

2024-02-13

我想打开 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>

通过这样做,它向我显示了一个包含两个应用程序的应用程序选择器:my app and 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:除非添加意图过滤器,否则电子邮件意图 ACTION_SENDTO 不起作用 的相关文章

随机推荐