Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_EMAIL, "emailaddress@emailaddress.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
startActivity(Intent.createChooser(intent, "Send Email"));
上面的代码打开一个对话框,显示以下应用程序:-蓝牙,谷歌文档,雅虎邮件,Gmail, Orkut, Skype等。
实际上,我想过滤这些列表选项。我只想显示电子邮件相关的应用程序,如Gmail和雅虎邮件。怎么做呢?
我曾在Android Market应用中看到过这样的例子。
打开Android Market应用程序
打开任何开发者指定了他/她的电子邮件地址的应用程序。(如果你找不到这样的应用程序,请打开我的应用程序:- market://details?id=com.becomputer06.vehicle.diary.free,或通过“车辆日记”搜索)
向下滚动到“开发者”
点击“发送电子邮件”
对话框只显示电子邮件应用程序,如Gmail,雅虎邮件等。它不显示蓝牙,Orkut等。什么样的代码产生这样的对话?
编辑:不再工作与Gmail的新版本
这是我当时发现的唯一方法,让它与任何角色一起工作。
doreamon的答案是正确的,因为它适用于Gmail新版本的所有角色。
旧的回答:
这是我的。它似乎适用于所有Android版本,支持主题和消息体,并支持完整的utf-8字符:
public static void email(Context context, String to, String subject, String body) {
StringBuilder builder = new StringBuilder("mailto:" + Uri.encode(to));
if (subject != null) {
builder.append("?subject=" + Uri.encode(Uri.encode(subject)));
if (body != null) {
builder.append("&body=" + Uri.encode(Uri.encode(body)));
}
}
String uri = builder.toString();
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
context.startActivity(intent);
}
只发送到电子邮件客户端-与多个附件
有很多解决方案,但都是局部有效的。
Mailto可以正确地过滤电子邮件应用程序,但它不能不发送流/文件。
Message /rfc822打开了大量的应用程序和电子邮件客户端
所以,这个问题的解决方法是两者都用。
首先使用mailto意图解析意图活动
然后将数据设置为每个已解析的活动以发送所需的数据
private void share()
{
Intent queryIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
Intent dataIntent = getDataIntent();
Intent targetIntent = getSelectiveIntentChooser(context, queryIntent, dataIntent);
startActivityForResult(targetIntent);
}
构建所需的数据意图,其中填充需要共享的数据
private Intent getDataIntent()
{
Intent dataIntent = buildIntent(Intent.ACTION_SEND, null, "message/rfc822", null);
// Set subject
dataIntent.putExtra(Intent.EXTRA_SUBJECT, title);
//Set receipient list.
dataIntent.putExtra(Intent.EXTRA_EMAIL, toRecipients);
dataIntent.putExtra(Intent.EXTRA_CC, ccRecipients);
dataIntent.putExtra(Intent.EXTRA_BCC, bccRecipients);
if (hasAttachments())
{
ArrayList<Uri> uris = getAttachmentUriList();
if (uris.size() > 1)
{
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
dataIntent.putExtra(Intent.EXTRA_STREAM, uris);
}
else
{
dataIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris.get(0));
}
}
return dataIntent;
}
protected ArrayList<Uri> getAttachmentUriList()
{
ArrayList<Uri> uris = new ArrayList();
for (AttachmentInfo eachAttachment : attachments)
{
uris.add(eachAttachment.uri);
}
return uris;
}
用于根据查询意图过滤所需意图的实用程序类
// Placed in IntentUtil.java
public static Intent getSelectiveIntentChooser(Context context, Intent queryIntent, Intent dataIntent)
{
List<ResolveInfo> appList = context.getPackageManager().queryIntentActivities(queryIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent finalIntent = null;
if (!appList.isEmpty())
{
List<android.content.Intent> targetedIntents = new ArrayList<android.content.Intent>();
for (ResolveInfo resolveInfo : appList)
{
String packageName = resolveInfo.activityInfo != null ? resolveInfo.activityInfo.packageName : null;
Intent allowedIntent = new Intent(dataIntent);
allowedIntent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name));
allowedIntent.setPackage(packageName);
targetedIntents.add(allowedIntent);
}
if (!targetedIntents.isEmpty())
{
//Share Intent
Intent startIntent = targetedIntents.remove(0);
Intent chooserIntent = android.content.Intent.createChooser(startIntent, "");
chooserIntent.putExtra(android.content.Intent.EXTRA_INITIAL_INTENTS, targetedIntents.toArray(new Parcelable[]{}));
chooserIntent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
finalIntent = chooserIntent;
}
}
if (finalIntent == null) //As a fallback, we are using the sent data intent
{
finalIntent = dataIntent;
}
return finalIntent;
}
根据Android开发者官方文档,这是发送电子邮件意图的正确方式
添加以下代码行到你的应用程序:
Intent intent = new Intent(Intent.ACTION_SEND);//common intent
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
可选:添加主体和主题,就像这样
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject Here");
intent.putExtra(Intent.EXTRA_TEXT, "E-mail body" );
你的问题里已经加了这句话
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"hello@example.com"});
这将是收件人的地址,这意味着用户将向您(开发人员)发送电子邮件。
主要有三种方法:
String email = /* Your email address here */
String subject = /* Your subject here */
String body = /* Your body here */
String chooserTitle = /* Your chooser title here */
1. 自定义Uri:
Uri uri = Uri.parse("mailto:" + email)
.buildUpon()
.appendQueryParameter("subject", subject)
.appendQueryParameter("body", body)
.build();
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(Intent.createChooser(emailIntent, chooserTitle));
2. 使用Intent附加:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, body);
//emailIntent.putExtra(Intent.EXTRA_HTML_TEXT, body); //If you are using HTML in your body text
startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
3.支持库ShareCompat:
Activity activity = /* Your activity here */
ShareCompat.IntentBuilder.from(activity)
.setType("message/rfc822")
.addEmailTo(email)
.setSubject(subject)
.setText(body)
//.setHtmlText(body) //If you are using HTML in your body text
.setChooserTitle(chooserTitle)
.startChooser();