Is开发者_如何学运维 there any URI which can point to the GMAIL App in android and help me launch it?
This works to intent just the gmail app.
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setType("plain/text")
.setData(Uri.parse("test@gmail.com"))
.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail")
.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"})
.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[] { "test@gmail.com" });
for single emails:
intent.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 Launch an application from another application on Android
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);
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\
Use this:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
startActivity(intent);
This could be device and API level dependent. Use with care.
Simple and 100 % working
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"anyMail@gmail.com"});
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();
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, ""));
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:" + "your_emailid@gmail.com"));
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.
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("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);
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);
The best way to do it, is by using the generic way/method:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject goes here");
intent.putExtra(Intent.EXTRA_TEXT, "Your Message goes here");
startActivity(Intent.createChooser(intent, ""));
This will give the users a choice where they can pick GMail (if installed) or any other email supporting app they have.
Please check the following code it'll open default mail composer automatically.
try {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
context.startActivity(intent);
} catch (android.content.ActivityNotFoundException anfe) {
handleException();
}
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+mailId)));
Working as for me is just simple like this:
Intent(Intent.ACTION_SEND).apply{
setPackage("com.google.android.gm")
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Go, go share text!")
}.also{readyIntent->
startActivity(readyIntent)
}
This snippet will open a chooser which is supposed to point to Gmail inbox.
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
try {
startActivity(Intent.createChooser(intent, getString(R.string.open_email_app)))
} catch (e: ActivityNotFoundException) {
showErrorDialog(R.string.error_activity_is_not_found)
}
Neither was working for me, as the subject and message were always empty, until I found the official solution here.
fun composeEmail(addresses: Array<String>, subject: String, attachment: Uri) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "*/*"
putExtra(Intent.EXTRA_EMAIL, addresses)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_STREAM, attachment)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
Yes its working code perfectly.....
Intent intent = new Intent(Intent.ACTION_SEND);
String[] strTo = { "test@g.com" };
intent.putExtra(Intent.EXTRA_EMAIL, strTo);
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
intent.setType("message/rfc822");
intent.setPackage("com.google.android.gm");
startActivity(intent);
This answer is old,but still appears in the Google Search at first position.
Hence,from the android documentation,the better way to do this now is :
public void composeEmail(String[] addresses, String subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, attachment);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
More information can be found out at here
final String package = "com.google.android.gm";
// return true if gmail is installed
boolean isGmailInstalled = isAppInstalled(MainActivity.this, package);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"mail@gamil.com"});
if (isGmailInstalled) {
intent.setType("text/html");
intent.setPackage(package);
startActivity(intent);
} else { // allow user to choose a different app to send email with
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "choose an email client"));
}
// Method to check if app is installed
private boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Intent URI to launch Gmail App
Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivityGmail");
startActivity(mailClient);
精彩评论