我做了一个小修改,使得你可以按名称列出想要分享的应用程序。这几乎与你已经发布的内容相同,只是添加了按名称分享应用程序的功能。
String[] nameOfAppsToShareWith = new String[] { "facebook", "twitter", "gmail" };
String[] blacklist = new String[]{"com.any.package", "net.other.package"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "some text");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a subject");
startActivity(generateCustomChooserIntent(intent, blacklist));
private Intent generateCustomChooserIntent(Intent prototype,
String[] forbiddenChoices)
{
List<Intent> targetedShareIntents = new ArrayList<Intent>();
List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
Intent chooserIntent;
Intent dummy = new Intent(prototype.getAction());
dummy.setType(prototype.getType());
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy,0);
if (!resInfo.isEmpty())
{
for (ResolveInfo resolveInfo : resInfo)
{
if (resolveInfo.activityInfo == null
|| Arrays.asList(forbiddenChoices).contains(
resolveInfo.activityInfo.packageName))
continue;
HashMap<String, String> info = new HashMap<String, String>();
info.put("packageName", resolveInfo.activityInfo.packageName);
info.put("className", resolveInfo.activityInfo.name);
String appName = String.valueOf(resolveInfo.activityInfo
.loadLabel(getPackageManager()));
info.put("simpleName", appName);
if (Arrays.asList(nameOfAppsToShareWith).contains(
appName.toLowerCase()))
{
intentMetaInfo.add(info);
}
}
if (!intentMetaInfo.isEmpty())
{
Collections.sort(intentMetaInfo,
new Comparator<HashMap<String, String>>()
{
@Override public int compare(
HashMap<String, String> map,
HashMap<String, String> map2)
{
return map.get("simpleName").compareTo(
map2.get("simpleName"));
}
});
for (HashMap<String, String> metaInfo : intentMetaInfo)
{
Intent targetedShareIntent = (Intent) prototype.clone();
targetedShareIntent.setPackage(metaInfo.get("packageName"));
targetedShareIntent.setClassName(
metaInfo.get("packageName"),
metaInfo.get("className"));
targetedShareIntents.add(targetedShareIntent);
}
String shareVia = getString(R.string.offer_share_via);
String shareTitle = shareVia.substring(0, 1).toUpperCase()
+ shareVia.substring(1);
chooserIntent = Intent.createChooser(targetedShareIntents
.remove(targetedShareIntents.size() - 1), shareTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
return chooserIntent;
}
}
return Intent.createChooser(prototype,
getString(R.string.offer_share_via));
}
这个解决方案和Makibo发布的几乎一样,只是添加了一个小功能,以便通过添加名称来轻松选择想要共享的应用程序,这样即使它们更改包名称或类似的东西,你也不会遇到任何问题。只要它们不更改名称。
getPackageManager().queryIntentActivities(shareIntent, 0);
获取到的应用程序顺序,请调用Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size()-1), "Select app to share");
,选择器会将其初始化的意图添加到 extra_initial_intents 的末尾 :) - BerťáktargetedShareIntent.setPackage(packageName)
(http://developer.android.com/reference/android/content/Intent.html#getPackage())非常重要,以便仅允许来自特定包的意图(这将是从目标共享意图中选择的一个)。然后附加自定义意图。为了进一步改进此代码,可以使用`targetedShareIntent.setClassName(packageName,className)`进行更详细的指定。 - Makibo