Lea*_* ES 1 email android android-intent
在 Android 上发送包含多个附件的电子邮件而不在选择器中包含非电子邮件应用程序的最佳方式是什么?
发送电子邮件时,我曾经这样做:
final Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@test.com" });
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,“message/rfc822”不再能够很好地从选择器中过滤掉不需要的应用程序,例如 Evernote、Drive 和各种其他应用程序。
我最近发现这个解决方法适用于单个附件:
sendEmailIntent = new Intent(Intent.ACTION_SENDTO);
Uri data = Uri.parse("mailto:?to=test@test.com&subject...");
sendEmailIntent.setData(data);
...
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不适用于多个附件。我试过了,Gmail 崩溃了。:S
我终于找到了一个解决方案,尽管该解决方案仅适用于 Ice Cream Sandwich MR1 及以上版本。诀窍是首先使用 ACTION_SEND_MULTIPLE 构建您的意图:
sendEmailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendEmailIntent.setType("message/rfc822");
sendEmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.com" });
sendEmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sendEmailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
final ArrayList<Uri> uris = /* ... Your code to build the attachments. */
sendEmailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
Run Code Online (Sandbox Code Playgroud)
要将其限制为仅适用于电子邮件应用程序,请添加以下代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
sendEmailIntent.setType(null); // If we're using a selector, then clear the type to null. I don't know why this is needed, but it doesn't work without it.
final Intent restrictIntent = new Intent(Intent.ACTION_SENDTO);
Uri data = Uri.parse("mailto:?to=some@email.com");
restrictIntent.setData(data);
sendEmailIntent.setSelector(restrictIntent);
}
Run Code Online (Sandbox Code Playgroud)
当您使用 startActivity() 触发此意图时,您现在只能在列表中看到电子邮件应用程序,如果您选择 Gmail,则多个附件将在那里。
我使用 try/catch 来执行此操作,以防 startActivity 解析为没有活动,在这种情况下,我删除了选择器,并且它似乎运行良好。
| 归档时间: |
|
| 查看次数: |
361 次 |
| 最近记录: |