Email url scheme For Android - android

I create button that use "mailto" to send email via email applications,when i click on the button it shows 2 option(gmail,outlook). Is there a possible way to show only the outlook application?

Yes, there is a possible way. You need to get package manager first. And iterate over the list of available matches that is received via queryIntentActivities() method. And find the app by passing package name of that outlook app. Code is below.
Intent outlookIntent = new Intent(Intent.ACTION_SEND);
outlookIntent.setType("text/html");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(outlookIntent, 0);
String outlookActivityClass = null;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.equals("com.microsoft.office.outlook")) {
outlookActivityClass = info.activityInfo.name;
if (outlookActivityClass != null && !outlookActivityClass.isEmpty()) {
break;
}
}
}
outlookIntent.setClassName("com.microsoft.office.outlook", outlookActivityClass);
outlookIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "yourmail#gmail.com" });
outlookIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
outlookIntent.putExtra(Intent.EXTRA_CC, "cc#gmail.com"); // if necessary
outlookIntent.putExtra(Intent.EXTRA_TEXT, "Email message");
outlookIntent.setData(Uri.parse("yourmail#gmail.com"));
startActivity(outlookIntent);
Hope this will help you out.

Related

How I open gmail directly from my application?

I have this code:
Intent dialogIntent = new Intent(android.content.Intent.ACTION_SEND);
dialogIntent.setType("plain/text");
dialogIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);
This opens a dialogue with DRIVE, GMAIL and SKYPE. I need to open gmail directly without the dialog appearing.
Already tried that and it does not work me. I'm doing this from a service.
add this to your intent :
dialogIntent.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
Note: this is not officially supported and may break in future versions. There is no documented or official way of launching the Gmail activity.
Edit: found another method, try and and see if it works:
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;
if (best != null)
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);

way to know if the user has chosen the intent action from chooser or not

Is there a way to know if the user has chosen the intent action from the chooser or not.
I want to do this - If its chosen by user then finish the current activity else remain in the current activity.
I have this code:
startActivity(Intent.createChooser(email, "Choose an Email client :"));
finish();
But this always finishes the current activity irrespective of user chose the email client or not.
Any ideas?
You can do this by showing you own custom chooser
First get all packages which can process your intent
private List<String> getInstalledComponentList(Intent emailIntent)
throws NameNotFoundException {
List<ResolveInfo> ril = getPackageManager().queryIntentActivities(emailIntent, 0);
List<String> componentList = new ArrayList<String>();
String name = null;
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
if (ri.activityInfo.labelRes != 0) {
name = res.getString(ri.activityInfo.labelRes);
} else {
name = ri.activityInfo.applicationInfo.loadLabel(
getPackageManager()).toString();
}
componentList.add(name);
}
}
return componentList;
}
Then show a dialog with all this list of packages like this
Then process the click event and start the selected package

Android: Check to see if Facebook app is present on user's device

I've kept a button which takes user to my facebook page.
In order that the official facebook app is opened I use the following url:
fb://pages/PAGE_ID
instead of http://facebook.com/PAGE_ID
Because in that case you get a list of browsers to open the url instead of teh facebook app.
It works if the user has facebook app installed. However it crashes if the user doesn't have facebook app.
Is there any way to check if the user has the facebook app?
Have you already checked this?
You can always check if an app is installed like this.
In an native android app, this is quite easy to achieve:
Uri dataUri = Uri.parse("fb://....");
Intent receiverIntent = new Intent(Intent.ACTION_VIEW, dataUri);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(receiverIntent, 0);
if (activities.size() > 0) {
startActivity(receiverIntent);
} else {
Uri webpage = Uri.parse("http://www.facebook.com/...");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
packageManager = getPackageManager();
activities = packageManager.queryIntentActivities(webIntent, 0);
if (activities.size() > 0) {
startActivity(webIntent);
}
}
I think you can reuse this code I wrote for checking if twitter app was installed on the device, to check if facebook app is installed. For twitterApps list you have to replace the values by "com.facebook.katana".
public Intent findTwitterClient() {
final String[] twitterApps = { "com.twitter.android", "com.handmark.tweetcaster", "com.seesmic", "com.thedeck.android", "com.levelup.touiteur", "com.thedeck.android.app" };
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "#hashtagTest");
tweetIntent.setType("text/plain");
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < twitterApps.length; i++) {
for (ResolveInfo resolveInfo : list) {
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(twitterApps[i])) {
tweetIntent.setPackage(p);
return tweetIntent;
}
}
}
return null;
}

How to open Gmail Compose when a button is clicked in Android App?

I am trying to open up Gmail Compose screen when a button is clicked in my Android App.
Do I need some API key for this from Google? or what do I need to do in my button onClickListener?
Any kind of insight is much appreciated.
As JeffC pointed out, it is easy to essentially tell Android that you want to send something email-like and have Android give users a list of choices, which will probably include GMail. If you specifically want GMail, you have to be a bit cleverer. (Note that the correct MIME type is actually "text/plain", not "plain/text". Do to an implementation oddity, GMail seems to be the only activity which responds to the latter, but this isn't a behavior I would count on.)
The following App demonstrates the principle you can follow: actually examine all of the activities which say they can handle your SEND intent and see if any of them look like GMail.
package com.stackoverflow.beekeeper;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import java.util.List;
public class StackOverflowTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
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;
if (best != null)
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
startActivity(intent);
}
}
try {
Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_email"));
intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
intent.putExtra(Intent.EXTRA_TEXT, "your_text");
startActivity(intent);
} catch (ActivityNotFoundException e){
//TODO smth
}
I don't know that you can specifically launch gmail. Have you tried this in your onClickListener
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
startActivity(emailIntent);
You can find more details here: Email android intent
You just place below code inside your click event. Will open directly gmail as compose mode, Output screenshot attached below.
Happy coding :-)
code :
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"mailto#gmail.com"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,"Subject text here...");
intent.putExtra(Intent.EXTRA_TEXT,"Body of the content here...");
intent.putExtra(Intent.EXTRA_CC,"mailcc#gmail.com");
intent.setType("text/html");
intent.setPackage("com.google.android.gm");
startActivity(Intent.createChooser(intent, "Send mail"));
Output :
Just Place the set of code in your click event/trigger event and it will directly navigate you to the native gmail application with all the details pre-filled.
All the email attributes/details are there in the set of code below(Comments added).
Intent intent = new Intent(Intent.ACTION_SEND);
String[] recipients = {"recipient#gmail.com"};//Add multiple recipients here
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject"); //Add Mail Subject
intent.putExtra(Intent.EXTRA_TEXT, "Enter your mail body here...");//Add mail body
intent.putExtra(Intent.EXTRA_CC, "mailcc#gmail.com");//Add CC emailid's if any
intent.putExtra(Intent.EXTRA_BCC, "mailbcc#gmail.com");//Add BCC email id if any
intent.setType("text/html");
intent.setPackage("com.google.android.gm");//Added Gmail Package to forcefully open Gmail App
startActivity(Intent.createChooser(intent, "Send mail"));
#HAPPY_CODING
public static void openGmail(Activity activity,String[] email, String subject, String content) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
final PackageManager pm = activity.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
ResolveInfo best = null;
for(final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
best = info;
if (best != null)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
activity.startActivity(emailIntent);
}
<TextView
android:id="#+id/EmailId"
android:linksClickable="true"
android:autoLink="email"
android:text="info#stackoverflow.com"
/>
This is the best method to send email on click of textView.
This code will directly start the gmail application to send an email.
I found out using this post that the important part here is to find the "packageName" and the "activityInfo.name"
I wanted to only use gmail without a chooser. Note that the package name is hard coded so if Gmail changes its packagename it won't work any more.
The key to this was the setComponent where the first param is the package name and the second param is the activityInfo name.
But like i said use with caution, I repeat myself; if the user doesn't have the gmail app installed or gmail changes its package name or Activty name to send an email this (hard)code will break. Thy have been warned ;)
Here is my code
Intent myIntent = new Intent(Intent.ACTION_SEND);
PackageManager pm = getPackageManager();
Intent tempIntent = new Intent(Intent.ACTION_SEND);
tempIntent.setType("*/*");
List<ResolveInfo> resInfo = pm.queryIntentActivities(tempIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
if (ri.activityInfo.packageName.contains("android.gm")) {
myIntent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"exampleto#gmail.com"});
myIntent.setType("message/rfc822");
myIntent.putExtra(Intent.EXTRA_TEXT, "extra text");
myIntent.putExtra(Intent.EXTRA_SUBJECT, "Extra subject");
myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("uri://your/uri/string");
}
}
startActivity(myIntent);
You can use Simple Intent.ACTION_SEND intent
set Intent.EXTRA_EMAIL for array of emails
set Intent.EXTRA_SUBJECT for subject line in email composer
Explore more EXTRA options available here -> https://developer.android.com/guide/components/intents-common#Email
Here's a quick code snippet
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
if (intent.resolveActivity(ctx.getPackageManager()) != null) {
startActivity(intent);
}
if you don't get anything in this line
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
then replace this line with
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 1);
Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain")
.putExtra(Intent.EXTRA_EMAIL, new String[]{emails});
List<ResolveInfo> matches = activity.getPackageManager().queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
best = info;
}
}
if (best != null) {
intent.setClassName(best.activityInfo.packageName,best.activityInfo.name);
}
activity.startActivity(intent);

Android email chooser

I am writing an app that needs to send emails at the end of each transaction. I am doing the following:
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("text/html");
mail.putExtra(Intent.EXTRA_EMAIL, new String[] { emailTo });
mail.putExtra(Intent.EXTRA_SUBJECT, "Send from Android");
mail.putExtra(Intent.EXTRA_TEXT, "Sent from Android");
startActivity(Intent.createChooser(mail,"Select Email Software..."));
What I would like to do is pre-select the email software and store it in a setting. That way, every time the email is being sent, it does not have to ask the user which email to use. I just can't seem to figure out how to invoke the chooser and get the selected value.
Any help would be greatly appreciated.
It's a common misconception to use text/plain or text/html. This will trigger any application that can handle plain or HTML text files without any context, including Google Drive, Dropbox, Evernote and Skype.
Instead use a ACTION_SENDTO, providing the mailto: Uri:
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
You can then proceed using the chooser as suggested through the other answers.
Here is the solution:
private void setSpinnerValues() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
PackageManager pm = getPackageManager();
emailers = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER);
if (emailers.size() == 0) {
spnEmailProgram.setEnabled(false);
return;
}
spnEmailProgram.setEnabled(true);
CharSequence[] sa = new CharSequence[emailers.size()];
int lastPosition = 0;
for (int i = 0; i < emailers.size(); i++) {
ResolveInfo r = emailers.get(i);
sa[i] = pm.getApplicationLabel(r.activityInfo.applicationInfo);
if (r.activityInfo.name.equalsIgnoreCase(Options.EmailClass)) {
lastPosition = i;
}
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, sa);
adapter.
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnEmailProgram.setAdapter(adapter);
spnEmailProgram.setSelection(lastPosition);
}
Save the choice for later use:
if (emailers.size() == 0) {
Options.EmailProgram = "";
Options.EmailClass = "";
} else {
ResolveInfo r = emailers.get(spnEmailProgram.getSelectedItemPosition());
Options.EmailProgram = r.activityInfo.packageName;
Options.EmailClass = r.activityInfo.name;
}
Now, to consume it, just to the following:
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("text/html");
Intent chooser = null;
if (Options.EmailProgram!=null && Options.EmailProgram.length()>0) {
mail.setClassName(Options.EmailProgram,Options.EmailClass);
chooser = mail;
}
fill in rest of data and start the activity
if (chooser == null) {
chooser = Intent.createChooser(mail,"Select Email Software...");
}
startActivity(chooser);
You would have to create your own chooser, possibly as an AlertDialog populated using the results of calling queryIntentActivities() on PackageManager.

Categories

Resources