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等。什么样的代码产生这样的对话?


当前回答

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", email, null));
if (emailIntent.resolveActivity(context.getPackageManager()) != null) {
    context.startActivity(Intent.createChooser(emailIntent, "Send Email..."));
} else {
    Toast.makeText(context, "No apps can perform this action.", Toast.LENGTH_SHORT).show();
}

其他回答

下面的代码对我有用!!

import android.support.v4.app.ShareCompat;
    .
    .
    .
    .
final Intent intent = ShareCompat.IntentBuilder
                        .from(activity)
                        .setType("application/txt")
                        .setSubject(subject)
                        .setText("Hii")
                        .setChooserTitle("Select One")
                        .createChooserIntent()
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

activity.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.content.Intent。带有数组的EXTRA_EMAIL。举个例子:

final Intent result = new Intent(android.content.Intent.ACTION_SEND);
result.setType("plain/text");
result.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { recipient });
result.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
result.putExtra(android.content.Intent.EXTRA_TEXT, body);

这对我来说很管用:

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "me@somewhere.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");

startActivity(Intent.createChooser(intent, "Email via..."));

最重要的是:使用ACTION_SENDTO动作而不是ACTION_SEND动作。我在一些Android 4.4设备上尝试了它,结果是:

它正确地限制选择弹出只显示电子邮件应用程序(电子邮件,Gmail,雅虎邮件等);而且 它正确地将电子邮件地址和主题插入到电子邮件中。

ACTION_VIEW也有一个简单的解决方案:

  Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.parse("mailto:customer@something.com?subject=Feedback");
        intent.setData(data);
        startActivity(intent);