Android 用于启动 Gmail 应用程序的意图 URI

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3470042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:19:21  来源:igfitidea点击:

Intent URI to launch Gmail App

androidandroid-intent

提问by Sana

Is there any URI which can point to the GMAIL App in android and help me launch it?

是否有任何 URI 可以指向 android 中的 GMAIL 应用程序并帮助我启动它?

回答by Jared Burrows

This works to intent just the gmail app.

这仅适用于 Gmail 应用程序。

final Intent intent = new Intent(Intent.ACTION_VIEW)
    .setType("plain/text")
    .setData(Uri.parse("[email protected]"))
    .setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail")
    .putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"})
    .putExtra(Intent.EXTRA_SUBJECT, "test")
    .putExtra(Intent.EXTRA_TEXT, "hello. this is a message sent from my demo app :-)");
startActivity(intent);

use for plenty of emails:

用于大量电子邮件:

intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });

for single emails:

对于单个电子邮件:

intent.setData(Uri.parse("[email protected]"));

You may add extra putExtra(Intent.EXTRA..)and change the setTypefor your purpose. :P

您可以根据自己的目的添加extra putExtra(Intent.EXTRA..)和更改setType。:P

Update (1/22/14):It is important to note that if you are going to use this code, you check to make sure that the user has the package "com.google.android.gm" installed on their device. In any language, make sure to check for null on specific Strings and initializations.

更新(14 年 1 月 22 日):请务必注意,如果您要使用此代码,请检查以确保用户在其设备上安装了“com.google.android.gm”软件包。在任何语言中,请确保检查特定字符串和初始化的 null。

Please see Launch an application from another application on Android

请参阅从 Android 上的另一个应用程序启动应用程序

enter image description here

在此处输入图片说明

回答by katzoft

I'm using this in my apps:

我在我的应用程序中使用它:

Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);

回答by Ajith Memana

Using package name is not recommended as its an undocumented method. In case if the package name changes some day the code will fail.

不建议使用包名,因为它是一种未记录的方法。如果包名称在某一天更改,代码将失败。

Try this code instead

试试这个代码

 Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
 "mailto", "[email protected]", null));
 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my subject text");
 context.startActivity(Intent.createChooser(emailIntent, null));

Ref: http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO\

参考:http: //developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO\

回答by Richard Lalancette

Use this:

用这个:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);

This could be device and API level dependent. Use with care.

这可能取决于设备和 API 级别。小心使用。

回答by CommonsWare

There is no documented and supported Intentfor launching Gmail -- sorry!

没有Intent关于启动 Gmail 的记录和支持——抱歉!

回答by Sana

Later the requirements changed to starting an "Email app", so the below code basically starts an email app and the user has to choose among the choices shown up.

后来要求更改为启动“电子邮件应用程序”,因此下面的代码基本上启动了一个电子邮件应用程序,用户必须在显示的选项中进行选择。

So, I had to use

所以,我不得不使用

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Emailing link");
intent.putExtra(Intent.EXTRA_TEXT, "Link is \n" +
        "This is the body of hte message");
startActivity(Intent.createChooser(intent, ""));

回答by CopsOnRoad

Simple and 100 % working

简单且 100% 工作

Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
intent.setPackage("com.google.android.gm");
if (intent.resolveActivity(getPackageManager())!=null)
    startActivity(intent);
else
    Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();

回答by Sunil

Try this

尝试这个

i have tried to many solutions but finally i got a correct way that works fine for me

我尝试了很多解决方案,但最终我得到了一个对我来说很好用的正确方法

try {
    Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "[email protected]"));
    intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
    intent.putExtra(Intent.EXTRA_TEXT, "your_text");
    startActivity(intent);
} catch(Exception e) {
    Toast.makeText(Share.this, "Sorry...You don't have any mail app", Toast.LENGTH_SHORT).show();
    e.printStackTrace(); 
}

Note

笔记

  • This will open your installed mail application(Email,Gmail) to send mail in which you can select one among them.
  • Don't use direct package name like("com.google.android.gm") because in future if they change package name your app will face problems.
  • 这将打开您安装的邮件应用程序(EmailGmail)以发送邮件,您可以在其中选择一个。
  • 不要使用像("com.google.android.gm") 这样的直接包名,因为将来如果他们更改包名,您的应用程序将面临问题。

回答by StephaneT

This trick work for ALL version (API 3+), as well as text/plainor text/html(by sonida) :

这个技巧适用于所有版本(API 3+),以及text/plaintext/html(by sonida):

Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
// 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;
        break;
    }
}
if (best != null) {
    intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
}
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "YOUR SUBJECT");
intent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("YOUR EXTRAS"));

startActivity(intent);

回答by Masanori Yono

It works.

有用。

Intent intent = new Intent(Intent.ACTION_SEND);

String[] strTo = { getString(R.string.mailto) };

intent.putExtra(Intent.EXTRA_EMAIL, strTo);
intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mail_subject));
intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_body));

Uri attachments = Uri.parse(image_path);
intent.putExtra(Intent.EXTRA_STREAM, attachments);

intent.setType("message/rfc822");

intent.setPackage("com.google.android.gm");

startActivity(intent);