20

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

我应该这样做Intent intent = new Intent(...)吗?

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

4

13 回答 13

39

如果目标是打开默认电子邮件应用程序以查看收件箱,那么关键是添加一个意图类别并使用 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

于 2015-07-23T17:58:16.077 回答
8

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

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);
    }
于 2015-01-28T10:40:53.277 回答
6

如果未配置设备中的默认邮件,有什么建议可以避免崩溃?

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

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


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

于 2011-11-29T09:16:12.067 回答
4

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

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
于 2020-06-29T03:00:56.427 回答
3

基于答案https://stackoverflow.com/a/28190156/3289338

从 Android 11 开始,系统不会为 queryIntentActivities 返回任何内容,因为我们首先需要像这样在查询中(在清单中)添加一个条目

<manifest ...>

<queries>
    ...
    <intent>
      <action
        android:name="android.intent.action.VIEW" />
      <data
        android:scheme="mailto" />
    </intent>
</queries>

...

</manifest>

这里是解决方案的 kotlin 版本:

fun Context.openMailbox(chooserTitle: String) {
    val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))

    val resInfo = packageManager.queryIntentActivities(emailIntent, 0)
    if (resInfo.isNotEmpty()) {
        // First create an intent with only the package name of the first registered email app
        // and build a picked based on it
        val intentChooser = packageManager.getLaunchIntentForPackage(
            resInfo.first().activityInfo.packageName
        )
        val openInChooser = Intent.createChooser(intentChooser, chooserTitle)

        // Then create a list of LabeledIntent for the rest of the registered email apps
        val emailApps = resInfo.toLabeledIntentArray(packageManager)

        // Add the rest of the email apps to the picker selection
        openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailApps)
        startActivity(openInChooser)
    } else {
        Timber.e("No email app found")
    }
}

private fun List<ResolveInfo>.toLabeledIntentArray(packageManager: PackageManager): Array<LabeledIntent> = map {
    val packageName = it.activityInfo.packageName
    val intent = packageManager.getLaunchIntentForPackage(packageName)
    LabeledIntent(intent, packageName, it.loadLabel(packageManager), it.icon)
}.toTypedArray()
于 2021-09-03T10:42:19.620 回答
2

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

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

于 2019-04-04T04:44:22.200 回答
1
  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); 
于 2011-11-12T05:39:59.907 回答
0

不幸的是,它看起来并不乐观。以前有人问过这个

如何将电子邮件客户端直接启动到收件箱视图?

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

于 2011-11-11T19:41:23.050 回答
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
于 2021-05-24T12:25:58.027 回答
0
Intent email = new Intent(Intent.ACTION_MAIN);
              
email.addCategory(Intent.CATEGORY_APP_EMAIL);
                    startActivity(email);
于 2019-07-05T17:43:58.753 回答
0

对于科特林:

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

于 2021-03-21T08:42:52.850 回答
0
val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(intent)
于 2022-01-18T19:37:29.657 回答
-2

您可以使用以下命令打开 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);
于 2012-10-29T12:54:32.793 回答