Here is the solution:
private void setSpinnerValues() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/html");
PackageManager pm = getPackageManager();
emailers = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER);
if (emailers.size() == 0) {
spnEmailProgram.setEnabled(false);
return;
}
spnEmailProgram.setEnabled(true);
CharSequence[] sa = new CharSequence[emailers.size()];
int lastPosition = 0;
for (int i = 0; i < emailers.size(); i++) {
ResolveInfo r = emailers.get(i);
sa[i] = pm.getApplicationLabel(r.activityInfo.applicationInfo);
if (r.activityInfo.name.equalsIgnoreCase(Options.EmailClass)) {
lastPosition = i;
}
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, sa);
adapter.
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnEmailProgram.setAdapter(adapter);
spnEmailProgram.setSelection(lastPosition);
}
Save the choice for later use:
if (emailers.size() == 0) {
Options.EmailProgram = "";
Options.EmailClass = "";
} else {
ResolveInfo r = emailers.get(spnEmailProgram.getSelectedItemPosition());
Options.EmailProgram = r.activityInfo.packageName;
Options.EmailClass = r.activityInfo.name;
}
Now, to consume it, just to the following:
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("text/html");
Intent chooser = null;
if (Options.EmailProgram!=null && Options.EmailProgram.length()>0) {
mail.setClassName(Options.EmailProgram,Options.EmailClass);
chooser = mail;
}
fill in rest of data and start the activity
if (chooser == null) {
chooser = Intent.createChooser(mail,"Select Email Software...");
}
startActivity(chooser);