如何在安卓设备中直接打开Gmail邮件撰写器?

3

我正在使用以下代码。问题是当我在设备上运行此代码时,它会打开一个包含3个选项的对话框,用于发送邮件。例如“POP,电子邮件,Gmail”等。 通过点击Gmail,邮件撰写器就会出现。 我只想直接显示Gmail邮件撰写器。而不是显示选择选项的对话框。请帮助我。

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");

String subject = "My Subject";

emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

emailIntent.setType("text/html");

String title = "<p align='center'>" + storyTitle + "<br/>" + storyPubDate + "</p>";

String data = "<p> Sent From ABC APP Sent from my Android </p>";

            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(title + data));
startActivity(Intent.createChooser(emailIntent, "Email:"));
6个回答

26

试试这个,完美

public void shareToGMail(String[] email, String subject, String content) {
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
    final PackageManager pm = activity.getPackageManager();
    final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
    ResolveInfo best = null;
    for(final ResolveInfo info : matches)
        if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
            best = info;
    if (best != null)
        emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
    activity.startActivity(emailIntent);
}

7

我不确定选择器的必要性。这是从我的应用程序之一中来的...


(注意:此处内容中包含HTML标签,已保留)

final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"jimblackler@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
activity.startActivity(intent);

2
请注意,setType 应该是 text/plain 而不是 plain/text(否则会出现 ActivityNotFoundException)。感谢您的提示! - Andrew

4

2
以下代码适用于我。它将搜索电子邮件客户端并直接启动新的电子邮件编辑器,并预填发送值。如果不存在电子邮件客户端,则应捕获该问题以避免崩溃。
这个解决方案好的地方是,在返回按钮上按下时,它会直接带您回到您的应用屏幕,从启动电子邮件意图开始。
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
    intent.putExtra(Intent.EXTRA_TEXT, "The message");
    try {
        startActivity(intent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "Mail account not configured", Toast.LENGHT_SHORT).show();
    }

谢谢!


0

您无法以编程方式绕过此弹出窗口。如果用户尝试访问该功能但未配置Gmail会发生什么?

如果您想要绕过这个弹出窗口,只需删除所有其他电子邮件客户端,这样Gmail就成为唯一一个可以发送/接收电子邮件的客户端,这样弹出窗口就不会再出现。


是的,如果Gmail没有配置,就不会有其他客户端。但是我该如何从我的设备中删除所有其他客户端? - Arslan Anwar

0
如果您的Android设备中有多个邮件撰写器,并且您只想使用Gmail撰写器进行请求,您需要:
emailIntent.setClassName("xxxgamil composer package name xxx", "xxxgmail composer class name xxx");  
    startActivity(emailIntent);

我找到了这段代码,它看起来像你的代码: Intent intent = new Intent(android.content.Intent.ACTION_SEND); intent.setType("text/plain"); final PackageManager pm = getPackageManager(); final List<ResolveInfo> matches = pm.queryIntentActivities(intent,0); ResolveInfo best=null; for (final ResolveInfo info : matches) if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) best=info; if (best!=null) intent.setClassName(best.activityInfo.packageName, best.activityInfo.name); startActivity(intent); - Arslan Anwar
完成后发送邮件后,它又显示了对话框。我不想再打开它了,有什么建议吗? - Arslan Anwar
如果您已经指定了类名,那么您应该直接获取composer活动,不是吗? - hxc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接