Possible to create an intent to show mail app options in android? - android

I would like to know if it is possible to have users open an email app of their choice without sending an email.
val intent = Intent(Intent.ACTION_SEND)
intent.type = "plain/text"
startActivity(Intent.createChooser(intent, "Choose Email"))
This allows the user to choose, but it opens the compose. I want just to open the email app.

This is a bit tricky, but not impossible.
Find all email clients
val resolveIntent = Intent(Intent.ACTION_SENDTO)
resolveIntent.setData(Uri.parse("mailto:default#recipient.com"))
val resolveInfoList = packageManager.queryIntentActivities(resolveIntent, PackageManager.MATCH_DEFAULT_ONLY)
Start email client
val intents = resolveInfoList.mapNotNull { info -> packageManager.getLaunchIntentForPackage(info.activityInfo.packageName) }.toMutableList()
if(intents.isEmpty()) {
//no mail client installed. Prompt user or throw exception
} else if(intents.size == 1) {
//one mail client installed, start that
startActivity(intents.first())
} else {
//multiple mail clients installed, let user choose which one to start
val chooser = Intent(Intent.ACTION_CHOOSER)
chooser.putExtra(Intent.EXTRA_INTENT, intents.removeAt(0))
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toTypedArray())
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(chooser)
}

Related

Android send user to email client chooser

I am trying to redirect user to check his email via available email clients. Currently what I have achieved is to send the user directly to Gmail client. Below there are some functions that I use to generate implicit intent:
fun attept1(): Intent {
val intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
)
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
return intent
}
fun attempt2(): Intent {
val intent = Intent(ACTION_VIEW)
intent.type = "message/rfc822"
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
return intent
}
//... somewhere in the code
val emailIntent = attempt1() //or attempt2()
startActivity(emailIntent)
With attempt 1 I am successfully redirecting the user to Gmail but I do have at least 5 email clients installed (BlueMail, Outlook, Yahoo Mail, Email, Temp Mail). They're ignored completely.
With attempt 2, at first it was showing unrelated apps like Slack, but after I have installed other email clients it just shows a toast message: Can't open this file. I have tried with Intent.createChooser(intent, "Choose client: ") but it didn't work either.

Open mail app via intent doesn't open chooser

I'm trying to open an mail application on Android via intent. The purpose is to have a button in the app that will open your mail inbox. However when I use:
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(Intent.createChooser(intent, "Email"))
It always opens gmail, while I also have Outlook installed. The only way I get to choose between the mail apps is when yo use the mailto. But I don't intent to send an email, so it is not desired to use that intent.
Doesn't Outlook support this Intent?
use the following snippet to open to outlook directly from a different app
context.startActivity(
Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName(
outlookLaunchIntent?.component?.packageName,
outlookLaunchIntent?.component?.className
)
setPackage(outlookLaunchIntent.package)
}
)
com.microsoft.office.outlook is the package name for outlook
For sending it to all email clients use a uri like this
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body);
intent.setData(data);
startActivity(intent);
Ref:How to open Email program via Intents (but only an Email program)

Open facebook link in chooser with browsers

Сan we open the facebook link so that the menu selection of applications for viewing the content was still and facebook browsers? The standard startup
fun openLink(link: String) {
Intent(Intent.ACTION_VIEW).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
data = Uri.parse(link)
context.startActivity( this)
}
}
opens only browsers, and some browsers, such as chrome and edge, run another one, where then appears facebook (if it is installed), the only thing I was able to find was to open the links directly to facebook, if it is installed, without the possibility of choosing an application to open the link
Intent(Intent.ACTION_VIEW).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
try {
val applicationInfo: ApplicationInfo =
packageManager.getApplicationInfo("com.facebook.katana", 0)
if (applicationInfo.enabled) {
// http://stackoverflow.com/a/24547437/1048340
data = Uri.parse("fb://facewebmodal/f?href=https://facebook.com/")
}
} catch (ignored: PackageManager.NameNotFoundException) {
data = Uri.parse("https://facebook.com/")
}
startActivity(this)
}
what i have
what i want and what chorme send after open facebook link
are you familiar with EXTRA_INITIAL_INTENTS? create two separate Intents (if fb app present and enabled) and join them
Intent chooserIntent = Intent.createChooser(browserIntent, "Open in...");
if (fbIntent != null) {
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { fbIntent });
}
startActivity(chooserIntent);
some more info in HERE and HERE

Pre fill SMS in facebook messenger

I would like to pre fill a SMS for user who uses facebook messenger.
Here you can find my Kotlin code :
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("sms:" + smsToString)
intent.putExtra("sms_body", provider.getSmsBody())
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val list = context!!.packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY)
val context = activity!!.applicationContext
if (list.isNotEmpty()) {
context.startActivity(intent)
}
This code launch the facebook messenger app with the new sms window, and with the list of contacts prefilled.
But, the sms_body is empty. The same code works when the default sms app is Android Messages.
Is there a solution ?
Thanks :)

Android Intent Chooser to only show E-mail option

My app integrates e-mail where the user can submit a bug report, feedback, etc. from the app directly. I'm using the application/octet-stream as the SetType for the Intent. When you go to submit the e-mail you get the content chooser and it shows various items from Evernote, Facebook, E-mail, etc.
How can I get this chooser to only show E-mail so as not to confuse the user with all these other items that fit the content chooser type?
Thank you.
To solve this issue simply follow the official documentation. The most important consideration are:
The flag is ACTION_SENDTO, and not ACTION_SEND.
The setData of method of the intent,
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
If you send an empty Extra, the if() at the end won't work and the app won't launch the email client.
This works for me. According to Android documentation. If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the ACTION_SENDTO action and include the "mailto:" data scheme. For example:
public void composeEmail(String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
https://developer.android.com/guide/components/intents-common.html#Email
I am presuming that you are using the ACTION_SEND Intent action, since you did not bother to actually state what you're using, but you agreed with #Aleadam's comment.
I'm using the application/octet-stream as the SetType for the Intent.
Nothing in that sentence limits things to email.
ACTION_SEND is a generic Intent action that can be supported by any application that wants to. All you do is indicate what data you are sharing and the MIME type of that data -- from there, it is up to the user to choose from available activities.
As #Jasoon indicates, you can try message/rfc822 as the MIME type. However, that is not indicating "only offer email clients" -- it indicates "offer anything that supports message/rfc822 data". That could readily include some application that are not email clients.
If you specifically want to send something by email, integrate JavaMail into your app, or write an email forwarding script on your Web server and invoke it, or something. If you use ACTION_SEND, you are implicitly stating that it is what the user wants that matters, and you want the user to be able to send such-and-so data by whatever means the user chooses.
Just struggled with this problem while implementing a Magic Link feature, a chooser intent for all installed email apps:
Chooser Intent Screenshot
private void openEmailApp() {
List<Intent> emailAppLauncherIntents = new ArrayList<>();
//Intent that only email apps can handle:
Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
emailAppIntent.setData(Uri.parse("mailto:"));
emailAppIntent.putExtra(Intent.EXTRA_EMAIL, "");
emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "");
PackageManager packageManager = getPackageManager();
//All installed apps that can handle email intent:
List<ResolveInfo> emailApps = packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL);
for (ResolveInfo resolveInfo : emailApps) {
String packageName = resolveInfo.activityInfo.packageName;
Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
emailAppLauncherIntents.add(launchIntent);
}
//Create chooser
Intent chooserIntent = Intent.createChooser(new Intent(), "Select email app:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toArray(new Parcelable[emailAppLauncherIntents.size()]));
startActivity(chooserIntent);
}
There is a way more generic to do that, working with any MIME type.
See this post: How to customize share intent in Android?
It is possible to limit the choices of an intent chooser to just a few options. The code in the answer to this question is a good example. In essence, you would have to create a List of LabeledIntents to provide to the intent chooser, that will then include it in its list. Note that this solution works not on exclusion (certain apps are excluded while the rest remain) but instead you have to pick which apps to display. Hope it helps!
It works on all devices. It will show only Email Apps
public static void shareViaMail(Activity activity, String title, String body, String filePath) {
Uri URI = Uri.parse("file://" + filePath);
final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"contact#brightsociety.com"});
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
try {
activity.startActivity(emailIntent);
} catch (Exception e) {
((BaseActivity) activity).showToast("Gmail App is not installed");
e.printStackTrace();
}
}
Kotlin Answer
If you need to show only email apps and then you want to open only inbox (not open new email writing), you need to do A and B:
A) Add below code in your AndroidManifest.xml file for Android 11 because of package visibility update of Android 11 :
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<intent>
<action android:name="android.intent.action.CHOOSER" />
</intent>
</queries>
B) Use below function to show email chooser:
// Show email app list.
fun showEmailAppList() {
// Email app list.
val emailAppLauncherIntents: MutableList<Intent?> = ArrayList()
// Create intent which can handle only by email apps.
val emailAppIntent = Intent(Intent.ACTION_SENDTO)
emailAppIntent.data = Uri.parse("mailto:")
// Find from all installed apps that can handle email intent and check version.
val emailApps = packageManager.queryIntentActivities(
emailAppIntent,
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) 0 else PackageManager.MATCH_ALL
)
// Collect email apps and put in intent list.
for (resolveInfo in emailApps) {
val packageName = resolveInfo.activityInfo.packageName
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
emailAppLauncherIntents.add(launchIntent)
}
// Create chooser with created intent list to show email apps of device.
val chooserIntent = Intent.createChooser(Intent(), "OPEN EMAIL APP")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toTypedArray())
startActivity(chooserIntent)
}
Result:
It works on all devices.It will show only Email Apps
public static void shareViaMail(Activity activity, String title, String body, String filePath) {
Uri URI = Uri.parse("file://" + filePath);
final Intent emailIntent = new Intent(Intent.ACTION_VIEW);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"xyz#gmail.com"});
/*if you want to attach something*/
if (URI != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
}
try {
activity.startActivity(emailIntent);
} catch (Exception e) {
((BaseActivity) activity).showToast("Gmail App is not installed");
e.printStackTrace();
}
}
Solution is very simple:
Intent testIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "blah blah subject" + "&body=" + "blah blah body" + "&to=" + "sendme#me.com");
testIntent.setData(data);
startActivity(testIntent);
See: http://www.gaanza.com/blog/email-client-intent-android/
After a lot of searching and testing, I finally found a perfect solution. Thanks to the Open source developer, cketti for sharing his/her concise and neat solution.
String mailto = "mailto:bob#example.org" +
"?cc=" + "alice#example.com" +
"&subject=" + Uri.encode(subject) +
"&body=" + Uri.encode(bodyText);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(mailto));
try {
startActivity(emailIntent);
} catch (ActivityNotFoundException e) {
//TODO: Handle case where no email app is available
}
And this is the link to his/her gist.

Categories

Resources