在createChooser中添加或删除选项

时间:2012-08-24 12:02:54

标签: android

String message = "Text I want to share";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share,"Share on"));

现在它显示默认选项,例如:Bluetooth, Email, Facebook, Gmail, LinkedIn, Messaging, Share Via Barcode.

或者这些是已安装的应用程序吗?

我想知道的是,我如何从这个列表中删除少数。我希望删除Share Via Barcode

并添加其他内容?

谢谢

2 个答案:

答案 0 :(得分:9)

使用以下代码将新项目添加到选配器屏幕。

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");       
share.putExtra(Intent.EXTRA_TEXT, message);

Intent addIntent = ;//whatever you want

Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, share );      
chooser.putExtra(Intent.EXTRA_TITLE, "title");

Intent[] intentArray =  {addIntent }; 
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivity(chooser);

但是无法删除特定项目。因此,您可以使用Packagemanager.resolveActivity解决意图并创建自己的自定义列表视图

答案 1 :(得分:3)

我使用以下代码获取设备上安装的所有电子邮件和短信应用的列表:

Intent shareSMS = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", "12346556", null));
shareSMS.addCategory(Intent.CATEGORY_DEFAULT);
shareSMS.putExtra("sms_body", message);

Intent shareEmail = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null));
PackageManager pm = getPackageManager();
List<ResolveInfo> mailActivityList = pm.queryIntentActivities(shareEmail, PackageManager.MATCH_DEFAULT_ONLY);

List<Intent> mailIntents = new ArrayList<Intent>();
for (ResolveInfo resInfo : mailActivityList) {
    Intent targetedOpenIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null))
            .setPackage(resInfo.activityInfo.packageName)
            .putExtra(Intent.EXTRA_EMAIL, emails)
            .putExtra(Intent.EXTRA_SUBJECT, subject)
            .putExtra(Intent.EXTRA_TEXT, message);
    mailIntents.add(targetedOpenIntent);
}

Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, shareSMS);
chooser.putExtra(Intent.EXTRA_TITLE, "Send request");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, mailIntents.toArray(new Parcelable[] { }));

startActivity(chooser);