0

I implemented sending emails from my application.

I used this quesion on SO as a guide to achieve this, note that in the answers someone said to use setType("message/rfc822"); because it filters out all other email client that listen to the ACTION_SEND intent.

My problem is that my galaxy tab 10.1 still has two application that listen to the intent, so a popup still opens asking me what email client I want to use. (The gMail app or the default eMail app). I can't uninstall one so the list won't popup, but I don't want to either.

Is there a way to force android to just use the first one in the list instantly? So the user can skip the popup dialog?

Here is my current code:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");

i.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.notes_from_pf));
i.putExtra(Intent.EXTRA_TEXT  , context.getString(R.string.mail_message));
try {
    startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Community
  • 1
  • 1
Pieter888
  • 4,882
  • 13
  • 53
  • 74
  • 3
    What makes you think that the user *wants* "the first one in the list"? – CommonsWare Jun 12 '12 at 11:45
  • Moreover, the code that you are using -- courtesy of the `Intent.createChooser()` call -- is *forcing* the user to have to make a choice, even if they set a default mail client earlier. Why are you simultaneously forcing the user to make a choice, then complaining that the user has to make a choice? – CommonsWare Jun 12 '12 at 11:50
  • @CommonsWare I so totally overlooked that... – Pieter888 Jun 12 '12 at 11:51
  • And so it is, I only get asked once now with an option to choose this client by default... This is alright for me, feel kind of stupid right now... – Pieter888 Jun 12 '12 at 11:53
  • http://commonsware.com/blog/2011/06/28/share-where-the-user-wants.html – CommonsWare Jun 12 '12 at 11:55
  • @CommonsWare Lesson learned. I'll still keep it this way though since my boss explicitly asked me to allow email only. – Pieter888 Jun 12 '12 at 12:00
  • Try looking at this answer: http://stackoverflow.com/a/17850303/624109 it uses the default activity (if such activity is defined for email). You can make some small changes to that so the popup will never be shown to the user and the first activity available (or the default defined by the user) will be used – Muzikant Aug 02 '13 at 11:52

3 Answers3

1

query applications, get a list of applications which are register to send action and choose one, create an intent, set class name and start your intent and you're done

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
0

if you wish to find which apps can handle the intent , you can use:

getPackageManager().queryIntentActivities...

when you find the app that you wish , set the package of the intent to match the one of the app.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("text/plain");
email.putExtra(Intent.EXTRA_EMAIL,  "" );

email.putExtra(Intent.EXTRA_SUBJECT, "");

email.putExtra(Intent.EXTRA_TEXT, prsnname + " :   "+ data  +"\n\n" +  linkdata);


try
{

activity.startActivity(Intent.createChooser(email, "Send mail..."));

}
catch (android.content.ActivityNotFoundException ex) 
{
    Toast.makeText(activity, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Dara Saini
  • 293
  • 1
  • 3
  • 15