3

How do I automatically get/pick the first Application, that handles a specified Intent, as if the user selected the first option in a createChooser() dialog.

In this example choose between applications that send data like emails:

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); SendEmailActivity.this.startActivity(Intent.createChooser(1, "Send mail..."));

Please help.

StrikeForceZero
  • 2,349
  • 1
  • 21
  • 35
Japz
  • 73
  • 2
  • 8

2 Answers2

5

using the answer to this question you can get a list of all the apps with intent android.content.Intent.ACTION_SEND

here's a working example I coded below (ends up picking gmail on my device)

**Fair warning - will throw a NullPointerException if no email accounts have been setup you should add a null check on the variable pkgAppsList and tell the user no email applications found or have been setup

    //set the main intent to ACTION_SEND for looking for applications that share information
    Intent intent = new Intent(Intent.ACTION_SEND, null);

    //intent.addCategory(Intent.CATEGORY_LAUNCHER); //if you want extra filters

    //filter out apps that are able to send plain text
    intent.setType("plain/text");

    //get a list of apps that meet your criteria above
    List<ResolveInfo> pkgAppsList = this.getPackageManager().queryIntentActivities( intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER);

    //select the first one in the list
    ResolveInfo info = pkgAppsList.get(0);      
    String packageName = info.activityInfo.packageName;
    String className = info.activityInfo.name;

    //set the intent to luanch that specific app
    intent.setClassName(packageName, className);

    //some samples on adding more then one email address
    String aEmailList[] = { "user@fakehost.com","user2@fakehost.com" };
    String aEmailCCList[] = { "user3@fakehost.com","user4@fakehost.com"};
    String aEmailBCCList[] = { "user5@fakehost.com" };

    //all the extras that will be passed to the email app       
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
    intent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
    intent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body.");

    //start the app
    startActivity(intent);

if you want to whitelist what app is being called you can loop through the list checking each package name for a specific package (example: gmail is "com.google.android.gm")

Community
  • 1
  • 1
StrikeForceZero
  • 2,349
  • 1
  • 21
  • 35
3

To add to what StrikeForceZero posted, I changed the auto-select-first selection to find the Gmail app from the list based on it's package name. Worked for me since the Gmail app wasn't always first on the list. Credit still goes to StrikeFirstZero...I just made a small mod to his code.

    //set the main intent to ACTION_SEND for looking for applications that share information
    Intent intent = new Intent(Intent.ACTION_SEND, null);

    //intent.addCategory(Intent.CATEGORY_LAUNCHER); //if you want extra filters

    //filter out apps that are able to send plain text
    intent.setType("plain/text");

    //get a list of apps that meet your criteria above
    List<ResolveInfo> pkgAppsList = activity.getPackageManager().queryIntentActivities( intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER);

    //Cycle through list of apps in list and select the one that matches GMail's package name
    for (ResolveInfo resolveInfo : pkgAppsList) {
        String packageName = resolveInfo.activityInfo.packageName;
        String className = "";
        if(packageName.equals("com.google.android.gm")) {
            className = resolveInfo.activityInfo.name;
            intent.setClassName(packageName, className);
        }
    }

    //some samples on adding more then one email address
    String aEmailList[] = { "user@fakehost.com","user2@fakehost.com" };
    String aEmailCCList[] = { "user3@fakehost.com","user4@fakehost.com"};
    String aEmailBCCList[] = { "user5@fakehost.com" };

    //all the extras that will be passed to the email app
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
    intent.putExtra(android.content.Intent.EXTRA_CC, aEmailCCList);
    intent.putExtra(android.content.Intent.EXTRA_BCC, aEmailBCCList);
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My subject");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "My message body.");

    //start the app
    activity.startActivity(intent);
The Hungry Androider
  • 2,158
  • 5
  • 24
  • 45