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



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

我应该使用Intent intent = new Intent(...)执行此操作吗?

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

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

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

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);
    }

基于答案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()

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

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

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


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

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

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

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

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

我使用的是jetpack compose,这对我来说很有效:

val context = LocalContext.current
val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
Button(
onClick = { startActivity(context, intent, null) }
)
  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); 

对于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

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

代码适用于我:

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

不幸的是,它看起来没有前景。之前已对此进行了询问

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

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

有点晚了,这里有合适的工作代码。

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

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

  1. 类别应用程序邮件
  2. makeMainSelectorActivity
Intent email = new Intent(Intent.ACTION_MAIN);
              
email.addCategory(Intent.CATEGORY_APP_EMAIL);
                    startActivity(email);

您可以使用以下命令打开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);

相关内容

  • 没有找到相关文章