微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我想用预格式化的电子邮件打开 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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。