在Android Studio中仅通过意图打开电子邮件应用程序

9
我不想向其他用户发送电子邮件。我只想打开“电子邮件”的“启动活动”。我已经尝试了不同的方法,但每次都会打开“发送邮件(撰写)”。 但是我只想打开“电子邮件”应用程序,没有其他任何东西(已发送、发件箱、垃圾邮件、废纸篓等)。 我的代码如下:
Intent intent = new Intent(Intent.ACTION_SENDTO)
    .setData(Uri.parse("mailto:"));

//check if the target app is available or not

if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

这段代码打开了"发送电子邮件到"(撰写)选项。但是我只想打开"电子邮件"应用程序,不要打开其他的(已发送、发件箱、垃圾邮件、已删除等)。


嗨,为什么你的帐户被暂时停用了?你做了什么导致这种情况发生了? - MMG
我有机会在做了500个重复后回顾问题。但是我没有任何理由地跳过,这就是版主给我的原因。哈哈 - Shoaib Kakal
1个回答

18

使用此代码打开默认的邮件应用程序:

try {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_EMAIL);
    this.startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
    Toast.makeText(Dashboard.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}

如果我需要打开其他不太出名的应用程序怎么办?每个应用程序都有类别吗? - Shoaib Kakal
你需要包名才能实现此操作,请参见以下链接:https://dev59.com/J2865IYBdhLWcg3wUs_z。 - Vishal Nagvadiya

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