Bhargav
Bhargav

Reputation: 8277

How to create a chooser for a list of applications using package manager

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

Upvotes: 1

Views: 643

Answers (2)

SaNtoRiaN
SaNtoRiaN

Reputation: 2202

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

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006674

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.

Upvotes: 2

Related Questions