如何从android代码打开默认邮件收件箱?

时间:2011-11-11 19:36:09

标签: android email android-intent inbox

我正在尝试将按钮链接到邮件应用。不发送邮件,只是打开收件箱。

我应该使用Intent intent = new Intent(...)吗?

如果是这样,( )之间应该是什么?

11 个答案:

答案 0 :(得分:19)

如果目标是打开默认电子邮件应用以查看收件箱,那么关键是添加意图类别并使用ACTION_MAIN意图,如下所示:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);

https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL

答案 1 :(得分:6)

是的,可以打开Android默认电子邮件收件箱 使用此代码:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.email");
startActivity(intent);


此代码有效,您必须先配置Android设备默认邮件。如果您已经配置了邮件,它可以正常工作。否则,强制关闭NullPointerException

答案 2 :(得分:3)

这段代码对我有用。它打开一个选择器,所有电子邮件应用程序都注册到设备并直接进入收件箱:

Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
    PackageManager pm = getPackageManager();

    List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0);
    if (resInfo.size() > 0) {
        ResolveInfo ri = resInfo.get(0);
        // First create an intent with only the package name of the first registered email app
        // and build a picked based on it
        Intent intentChooser = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
        Intent openInChooser =
                Intent.createChooser(intentChooser,
                        getString(R.string.user_reg_email_client_chooser_title));

        // Then create a list of LabeledIntent for the rest of the registered email apps 
        List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
        for (int i = 1; i < resInfo.size(); i++) {
            // Extract the label and repackage it in a LabeledIntent
            ri = resInfo.get(i);
            String packageName = ri.activityInfo.packageName;
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }

        LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
        // Add the rest of the email apps to the picker selection
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        startActivity(openInChooser);
    }

答案 3 :(得分:3)

要打开新任务,请使用以下代码:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

答案 4 :(得分:2)

  You can use this but it is for gmail only

  Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);  
  emailIntent.setType("plain/text");
  startActivity(emailIntent); 

答案 5 :(得分:0)

不幸的是,这看起来并不乐观。这是在

之前提出的

How do I launch the email client directly to inbox view?

您可以在撰写模式下打开电子邮件客户端,但您似乎已经知道了。

答案 6 :(得分:0)

您可以使用以下方法打开Android默认电子邮件客户端:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.Welcome");
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(emailIntent);

答案 7 :(得分:0)

没有附件时,您可以简单地使用以下代码:

Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:support@mailname.com")); 
i.putExtra(Intent.EXTRA_SUBJECT, "Feedback/Support");
startActivity(Intent.createChooser(emailIntent, "Send feedback"));

有关详细信息,我建议访问: https://developer.android.com/guide/components/intents-common.html#Email

答案 8 :(得分:0)

意图电子邮件=新意图(Intent.ACTION_MAIN);

email.addCategory(Intent.CATEGORY_APP_EMAIL);                     startActivity(email);

答案 9 :(得分:0)

对于 kotlin:

fun composeEmail(addresses: Array<String>, subject: String) {
    val intent = Intent(Intent.ACTION_SENDTO).apply {
        data = Uri.parse("mailto:") // only email apps should handle this
        putExtra(Intent.EXTRA_EMAIL, addresses)
        putExtra(Intent.EXTRA_SUBJECT, subject)
    }
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }
}

参考:https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL

答案 10 :(得分:0)

有点晚了,这是正确的工作代码。

Intent intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Email"));

有关更多详细信息,请查看此文档:

  1. CATEGORY_APP_EMAIL
  2. makeMainSelectorActivity