Anda di halaman 1dari 7

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

12

Intent URI to launch Gmail App


android

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

share

improve this question


Sana
5,631 12 40 74

Asked
Aug 12 '10 at 16:49

jball
18k 7 49 81

Edited
Aug 12 '10 at 16:53

8 Answers

15

Order By

Votes

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);

share

improve this answer


katzoft
448 2 10

Answered
Aug 28 '10 at 19:07

Excellent answer! I was looking for it since a long time. Sana Aug 29 '10 at 17:47

i searched for 4 hours and now i got my ans ,,, thanks a ton djk Mar 24 '11 at 12:26

12

That is undocumented, unsupported, and may well break in the future. CommonsWare Sep 18 '11 at 12:28
I think it's best to wrap that code in a try catch Marco Matarazzi Sep 4 '12 at 13:30

Targeting API 17 I'm getting a


android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.a
ndroid.gm/com.google.android.gm.ConversationListActivity}; have you declared this activity in y
our AndroidManifest.xml?

Any ideas? Garret Wilson Nov 16 '12 at 21:59


show 8 more comments

This works to intent just the gmail app.

30

Intent sendIntent = new Intent(Intent.ACTION_VIEW);


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

use for plenty of emails: sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {


"test@gmail.com" }); for single emails: sendIntent.setData(Uri.parse("test@gmail.com"));
You may add extra "putExtra(Intent.EXTRA..)" and change the "setType" for your purpose. :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.
Please see Android Launch an application from another application

share

improve this answer


Jared Burrows
21.8k 10 78 108

Answered
Sep 17 '11 at 16:20

Edited
Mar 10 '14 at 17:07

Why is there a minus 1? This code works... Jared Burrows Oct 2 '11 at 3:49

works nicely! also sendIntent.setData(Uri.parse("abc@gmail.com")); seems to be needless. Aleksey Malevaniy Dec


26 '11 at 16:50

Does anyone know why it has to be new String[] { "abc@gmail.com" } instead of just "abc@gmail.com" for the 2nd
parameter? anon58192932 Jul 7 '12 at 6:03

It is a String array, it allows for multiple emails to be added. Jared Burrows Jul 7 '12 at 6:20

There are downvotes because setting the package manually to "com.google.android.gm" is undocumented, and could
potentially break if the package name ever changes or is not installed. LocalPCGuy Jan 22 '14 at 19:00
show 3 more comments

I tried so many things today, I was getting frustrated ahaha. I just wanted to launch the email
client... this is what solved it for me:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);

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

share

improve this answer


Richard Lalancette
1,565 12 23

Answered
Aug 30 '11 at 20:44

Edited
Apr 13 '14 at 14:19

share

There is no documented and supported Intent for launching Gmail -- sorry!

improve this answer


CommonsWare
564k 75 1336 1392

Answered
Aug 12 '10 at 17:45

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, ""));

share

improve this answer


Sana
5,631 12 40 74

Answered
Jun 20 '11 at 19:47

Tim S. Van Haren


6,960 19 33

Edited
Dec 31 '14 at 18:23

If you see clearly what droid_fan has answered then it launches email app only on particular devices but my answer launches
the email app no matter what the platform is. Sana Sep 18 '11 at 1:29
+1 for the alternative. Having looked again perhaps Richard Lalancette's answer provides a more generic solution for launching
packages with unknown launch intent details. Merlin Sep 18 '11 at 2:03
add a comment

This trick work for ALL version (API 3+), as well as text/plain or text/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("g
mail")) {
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);

share

improve this answer


StephaneT
262 3 10

Answered
Mar 12 '13 at 12:42

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", "abc@gmail.com", 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\

share

improve this answer


Ajith Memana
1,221 11 27

Answered
Feb 11 '14 at 11:29

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);

share

improve this answer


Masanori Yono
1 1

Answered
Jan 29 '15 at 1:56

Edited
Jan 29 '15 at 3:07

Your Answer

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

Anda mungkin juga menyukai