如何在createchooser上自动选择电子邮件应用程序?

时间:2012-09-06 01:19:12

标签: android android-intent

如何自动获取/选择处理指定Intent的第一个Application,就好像用户在createChooser()对话框中选择了第一个选项一样。

在此示例中,选择发送电子邮件等数据的应用程序:

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

请帮忙。

2 个答案:

答案 0 :(得分:4)

使用answer到此question,您可以获得有意图的所有应用的列表android.content.Intent.ACTION_SEND

这是我在下面编写的一个工作示例(最终在我的设备上选择gmail)

**公平警告 - 如果没有设置电子邮件帐户,将抛出NullPointerException 你应该对变量pkgAppsList添加空检查并告诉用户没有找到或已经设置的电子邮件应用程序

    //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);

如果要将正在调用的应用程序列入白名单,您可以遍历列表,检查特定包的每个包名称(例如:gmail是&#34; com.google.android.gm&#34;)

答案 1 :(得分:2)

要添加StrikeForceZero发布的内容,我更改了自动选择优先选择,以便根据列表名称从列表中查找Gmail应用。为我工作,因为Gmail应用程序并不总是在列表中排在第一位。还是继续使用StrikeFirstZero ...我只是为他的代码做了一个小模块。

    //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);