安卓手机如何直接打开邮件客户端

7

我想要打开默认的电子邮件客户端,而不是显示选项。我尝试了但是没有成功,请有经验的人帮忙。

我使用了以下代码:

  final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

        emailIntent.setType("text/html");
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Allergy Journal");       
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<small>"+sb.toString()+"</small>"));                                 
        startActivity(Intent.createChooser(emailIntent, "Email:"));   

这显示了选项:

enter image description here

但我想直接打开默认的电子邮件客户端。

enter image description here

2个回答

15

按照以下格式组织字符串:String URI="mailto:?subject=" + subject + "&body=" + body;

并且

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse(URI);
intent.setData(data);
startActivity(intent);

这将打开用户选择的默认电子邮件程序。

Linkify是这样做的。如果您愿意,请查看它的源代码


8
您可以使用以下代码打开您想要的任何intent,例如gmail、facebook、email等。在我的代码中,如果您想要打开gmail,请在类型中简单地传递"gmail",如果您想要打开Facebook,请传递"face"。
Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
intent.setType("text/html");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);

if (!resInfo.isEmpty())
{
    for (ResolveInfo info : resInfo) 
    {
    if (info.activityInfo.packageName.toLowerCase().contains(type) || info.activityInfo.name.toLowerCase().contains(type)) 
    {
            intent.putExtra(android.content.Intent.EXTRA_TEXT, htmlBody);
            intent.setPackage(info.activityInfo.packageName);   
            startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));
        }
} 

我想打开Android电子邮件客户端。为此,我应该使用什么字符串代替类型? - naresh
嗨,@AndroidDev,你能不能帮忙查一下这个问题:https://dev59.com/MaDia4cB1Zd3GeqPAkHt。谢谢。 - Faizal Munna

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