1

Basically I have a list of package names for popular email apps, and I want to create a chooser to launch the send email intent, you can refer to this question, here he uses the package manager for just gmail, I want to do it for a list of packages

Community
  • 1
  • 1
Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • "Basically I have a list of package names for popular email apps" -- please allow the user to use whatever email application that the user wants. This may include applications on in your list. Or, please explain why it is in the *user's* best interests to be restricted to your list. – CommonsWare Jan 06 '16 at 14:42
  • @CommonsWare I do this because to my knowledge (according the question linked in this question) there is no proper Intent action specifically targetting Email applications or more correctly - common Intent action to which all the world's email applications listen to – Bhargav Jan 06 '16 at 14:46
  • "launch the send email intent" -- if you want to *send an email*, use `ACTION_SENDTO` (if you have the email address) or `ACTION_SEND` (if you do not). You do not need to use `PackageManager`, or have a list of email app packages. In the question that you linked to, the OP does *not* want to "launch the send email intent". The OP wrote, in boldface, "**I don't want to send a new message**". – CommonsWare Jan 06 '16 at 14:57
  • What I want to do is simulate the desktop behavior when you click on an email link which opens outlook/gmail client with the to field set to the email id, but in addition to this I want to let the user choose the email application that is launched – Bhargav Jan 06 '16 at 14:59

2 Answers2

2

What I want to do is simulate the desktop behavior when you click on an email link which opens outlook/gmail client with the to field set to the email id, but in addition to this I want to let the user choose the email application that is launched

startActivity(new Intent(Intent.ACTION_SENDTO)
  .setData(Uri.parse("mailto:"+yourEmailAddressGoesHere)));

where you replace yourEmailAddressGoesHere with "the email id".

If the user has more than one email client, and the user has not chosen a default email client, the user will get a chooser automatically. If the user has only one email client, or has chosen a default email client, this will lead the user to some activity to compose a message to your designated email address.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yes thats what I wanted, so I use a try catch block with this to show error when there are no email clients present yes? And are you sure this only caters to email applications? – Bhargav Jan 06 '16 at 15:10
  • @Bhargav: "so I use a try catch block..." -- yes. You can also create the `Intent` and call `queryIntentActivities()` on `PackageManager`, passing in that `Intent`. If `queryIntentActivities()` returns a zero-length list, you know that you would get an `ActivityNotFoundException` if you tried calling `startActivity()` on the `Intent`. – CommonsWare Jan 06 '16 at 15:13
  • @Bhargav: "And are you sure this only caters to email applications?" -- any app with an activity that implements `ACTION_SENDTO` for `mailto` schemes will be an option for the user. That's usually just email apps. More importantly, though, the mix of apps is up to the user, not you. You are not more important than your users. If your users want to use something else, and that something else appears in the chooser, the user is welcome to choose it. – CommonsWare Jan 06 '16 at 15:14
  • Alright your 2nd comment clears things up for me thanks, im selecting this the right answer, if you could just try to append your 2nd comment to the answer – Bhargav Jan 06 '16 at 15:17
  • For me the Action_SendTo shows no app in chooser, But Action_Send worked. Thanks for the Data* hint – SaadurRehman May 17 '22 at 06:12
2

Create the intent add set its uri data

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);

then create the chooser intent. It's useful when you want the user to choose the app he wants every time he sends an email. If you want to choose an app and make it a default, you can omit the chooser and start the intent directly

Intent chooser = Intent.createChooser(intent, "Chooser title");

then check if there is at least one activity that can handle the email intent

if(intent.resolveActivity(getPackageManager()) != null){
    // there are apps, start the chooser
    startActivity(chooser);
} else {
    // no apps found
    Toast.makeText(this, "No apps found", Toast.LENGTH_SHORT).show();
}
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
  • You shouldn't use those Extras with ACTION_SENDTO because they're only documented for ACTION_SEND and ACTION_SEND_MULTIPLE. Also, I highly recommend not using a chooser with ACTION_SENDTO. More details here: https://medium.com/@cketti/android-sending-email-using-intents-3da63662c58f – cketti Jan 12 '16 at 00:32
  • Respect your pov and very good explanation, but I have another view. First, you mentioned they also work for that action, and I think it's clearer to use them as extras instead of merging in the uri. Second, I may use gmail but sometimes I use yahoo for example so I think a chooser in this case is useful. – SaNtoRiaN Jan 12 '16 at 00:47
  • When not using `Intent.createChooser()` users don't have to select a default app. In that case they will see a chooser dialog every time. But if you as a developer of the app decide to use `Intent.createChooser()` the user won't have the option to select a default app. – cketti Jan 12 '16 at 01:05
  • Yeah I know, I think not all people like choosing a default thing, they want to see a chooser every time although they always choose the same app. I'm an android user before being a developer, also I see many people doing the same. I think default app maybe useful in a browsers or apps that really does not need a chooser. – SaNtoRiaN Jan 12 '16 at 01:13