3

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

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

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

请帮忙。

4

2 回答 2

5

使用这个问题的答案,您可以获得所有有意图的应用程序的列表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 是“com.google.android.gm”)

于 2012-09-06T01:32:33.657 回答
3

为了添加 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);
于 2014-08-17T10:25:02.270 回答