Force android to pick an app for email sending

2019-09-17 22:56发布

问题:

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();
}

回答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



回答2:

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.



回答3:

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();
}