【发布时间】:2011-12-27 07:03:15
【问题描述】:
我正在尝试将按钮链接到邮件应用程序。不是发送邮件,只是打开收件箱。
我应该用Intent intent = new Intent(...) 这样做吗?
如果是这样,( ) 之间应该是什么?
【问题讨论】:
标签: android email android-intent inbox
我正在尝试将按钮链接到邮件应用程序。不是发送邮件,只是打开收件箱。
我应该用Intent intent = new Intent(...) 这样做吗?
如果是这样,( ) 之间应该是什么?
【问题讨论】:
标签: android email android-intent inbox
不幸的是,它看起来并不乐观。之前有人问过这个问题
How do I launch the email client directly to inbox view?
您可以在撰写模式下打开电子邮件客户端,但您似乎已经知道这一点。
【讨论】:
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);
【讨论】:
Intent mailClient = new Intent(Intent.ACTION_VIEW); mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity"); startActivity(mailClient);
如果没有配置设备中的默认邮件,有什么建议可以避免崩溃?
是的,可以打开 Android 默认电子邮件收件箱。
使用此代码:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.email");
startActivity(intent);
此代码有效,您必须先配置您的 Android 设备默认邮件。如果您已经配置了邮件,它可以正常工作。否则,它会以NullPointerException 强制关闭。
【讨论】:
您可以使用以下方式打开 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);
【讨论】:
这段代码对我有用。它会打开一个选择器,其中包含所有注册到设备并直接发送到收件箱的电子邮件应用程序:
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);
}
【讨论】:
如果目标是打开默认电子邮件应用以查看收件箱,那么关键是添加一个意图类别并使用 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
【讨论】:
emailActivity打开IN你当前的堆栈(即在你的应用程序中),导致坏用户exp。要在新窗口中打开email,请在启动前添加行intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)。
如果没有附件,您可以简单地使用以下代码:
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
【讨论】:
Intent email = new Intent(Intent.ACTION_MAIN);
email.addCategory(Intent.CATEGORY_APP_EMAIL);
startActivity(email);
【讨论】:
要打开新任务,请使用以下代码:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
【讨论】:
对于 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
【讨论】:
有点晚了,这里是正确的工作代码。
Intent intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Email"));
欲了解更多详情,请查看此文档:
【讨论】:
基于答案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()
【讨论】: