有没有办法以编程方式打开电子邮件客户端,而无需强制发送消息?我只是希望该应用程序让用户打开他的电子邮件客户端以进行电子邮件检查:)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, ""));
此代码有效,但它强制用户发送新消息。
有没有办法以编程方式打开电子邮件客户端,而无需强制发送消息?我只是希望该应用程序让用户打开他的电子邮件客户端以进行电子邮件检查:)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, ""));
此代码有效,但它强制用户发送新消息。
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
startActivity(intent);
startActivity(Intent.createChooser(intent, getString(R.string.ChoseEmailClient)));
这有点奏效。但它为我打开了 Gmail,即使我有其他电子邮件客户端
此代码将显示一个包含电子邮件客户端列表的对话框。单击一个将启动应用程序:
try {
List<String> emailClientNames = new ArrayList<String>();
final List<String> emailClientPackageNames = new ArrayList<String>();
// finding list of email clients that support send email
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "abc@gmail.com", null));
PackageManager pkgManager = AppController.getContext().getPackageManager();
List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0);
if (!packages.isEmpty()) {
for (ResolveInfo resolveInfo : packages) {
// finding the package name
String packageName = resolveInfo.activityInfo.packageName;
emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString());
emailClientPackageNames.add(packageName);
}
// a selection dialog for the email clients
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Select email client");
builder.setItems(emailClientNames.toArray(new String[]{}), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// on click we launch the right package
Intent intent = getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which));
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
} catch (ActivityNotFoundException e) {
// Show error message
}
在 Kotlin 中,但此版本为用户创建了一个选择器来选择要使用的电子邮件应用程序。除了使用LabeledIntent
.
fun emailAppIntent(): Intent? {
val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))
val packageManager = appLaunchContext.packageManager
val activitiesHandlingEmails = packageManager.queryIntentActivities(emailIntent, 0)
if (activitiesHandlingEmails.isNotEmpty()) {
// use the first email package to create the chooserIntent
val firstEmailPackageName = activitiesHandlingEmails.first().activityInfo.packageName
val firstEmailInboxIntent = packageManager.getLaunchIntentForPackage(firstEmailPackageName)
val emailAppChooserIntent = Intent.createChooser(firstEmailInboxIntent, "")
// created UI for other email packages and add them to the chooser
val emailInboxIntents = mutableListOf<LabeledIntent>()
for (i in 1 until activitiesHandlingEmails.size) {
val activityHandlingEmail = activitiesHandlingEmails[i]
val packageName = activityHandlingEmail.activityInfo.packageName
val intent = packageManager.getLaunchIntentForPackage(packageName)
emailInboxIntents.add(
LabeledIntent(
intent,
packageName,
activityHandlingEmail.loadLabel(packageManager),
activityHandlingEmail.icon
)
)
}
val extraEmailInboxIntents = emailInboxIntents.toTypedArray()
return emailAppChooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraEmailInboxIntents)
} else {
return null
}
}
..然后稍后调用它
val emailChooserIntent = emailAppIntent()
if (emailChooserIntent != null) {
startActivity(emailAppIntent())
}
或根据需要处理 null 。
我认为您应该替换Intent.ACTION_SEND
为Intent.ACTION_VIEW
,
我相信这会起作用,因为这将提示支持 MIME 类型的应用程序列表,"message/rfc822"
因此它将在您的设备中包含您的默认电子邮件客户端,而不是 gmail 应用程序。
这段代码怎么样:
final Intent emailLauncher = new Intent(Intent.ACTION_VIEW);
emailLauncher.setType("message/rfc822");
try{
startActivity(emailLauncher);
}catch(ActivityNotFoundException e){
}
Kotlin 中更现代的解决方案
fun tryVerifyMail() {
try {
val intents: List<Intent> = (packageManager.queryIntentActivities(Intent(
Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "lowhillgamesoy@gmail.com", null
)
), 0) + packageManager.queryIntentActivities(Intent(Intent.ACTION_VIEW).also {
it.type = "message/rfc822"
}, 0)).mapNotNull {
it.activityInfo.packageName
}.toSet().mapNotNull {
packageManager.getLaunchIntentForPackage(it)
}
if(intents.size > 0) {
startActivityForResult(Intent.createChooser(intents.first(), getString(R.string.verify_mail)).also {
if(intents.size > 1) {
it.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.subList(1, intents.size - 1).toTypedArray())
}
}, OPEN_MAIL_REQUEST_CODE)
} else {
Toast.makeText(this, "Verify your e-mail by clicking the link in your e-mail inbox.", Toast.LENGTH_LONG).show()
}
} catch (e: ActivityNotFoundException) {
// Show error message
e.printStackTrace()
}
}
这也会选择仅支持 ACTION_SEND 和 ACTION_VIEW 的邮件客户端并删除重复项。