我正在尝试将按钮链接到邮件应用程序。不是发送邮件,只是打开收件箱。
我应该这样做Intent intent = new Intent(...)
吗?
如果是这样,两者之间应该是什么( )
?
我正在尝试将按钮链接到邮件应用程序。不是发送邮件,只是打开收件箱。
我应该这样做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);
}
如果未配置设备中的默认邮件,有什么建议可以避免崩溃?
是的,可以打开 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);
基于答案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()
如果没有附件,您可以简单地使用以下代码:
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
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 intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Email"));
有关更多详细信息,请查看此文档:
Intent email = new Intent(Intent.ACTION_MAIN);
email.addCategory(Intent.CATEGORY_APP_EMAIL);
startActivity(email);
对于科特林:
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)
您可以使用以下命令打开 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);