I have used the following codes to exclude facebook app from my app chooser:
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("image/*");
List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("image/*");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "tooter.nl");
if (!TextUtils.equals(packageName, "com.facebook.katana")) {
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, st);
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
startActivity(chooserIntent);
}
Facebook app was removed when I used the code. But the sad part was, even Twitter app was also removed from the app chooser and other unnecessary apps like "Android system" was listed on the chooser . What should i do ? Is something missing or wrong with this code ?
Check my answer below. It will exclude only facebook application from sharing.
void shareOnOtherSocialMedia() {
List<Intent> shareIntentsLists = new ArrayList<Intent>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
List<ResolveInfo> resInfos = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resInfo : resInfos) {
String packageName = resInfo.activityInfo.packageName;
if (!packageName.toLowerCase().contains("facebook")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.setPackage(packageName);
shareIntentsLists.add(intent);
}
}
if (!shareIntentsLists.isEmpty()) {
Intent chooserIntent = Intent.createChooser(shareIntentsLists.remove(0), "Choose app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, shareIntentsLists.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
} else
Log.e("Error", "No Apps can perform your task");
}
}
}
And call the above function wherever you want.
Let me know for queries.
Android N (API 24) introduces blacklist Intent.EXTRA_EXCLUDE_COMPONENTS, which is better and simpler than whitelist Intent.EXTRA_INITIAL_INTENTS.
Updated Kotlin answer based on Ragu Swaminathan's answer
fun share() {
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.type = "text/plain"
val resInfoList = activity?.packageManager?.queryIntentActivities(shareIntent, 0)
val shareIntentList = arrayListOf<Intent>()
if (resInfoList?.isNotEmpty() == true) {
for (resInfo in resInfoList) {
val packageName = resInfo.activityInfo.packageName
if (!packageName.toLowerCase().contains("discord")) {
val intent = Intent()
intent.component = ComponentName(packageName, resInfo.activityInfo.name)
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.`package` = packageName
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
shareIntentList.add(intent)
}
}
}
if (shareIntentList.isEmpty()) {
Toast.makeText(activity, "No apps to share!", Toast.LENGTH_LONG).show()
} else {
val chooserIntent = Intent.createChooser(Intent(), "Choose app to share")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, shareIntentList.toTypedArray())
activity?.startActivity(chooserIntent)
}
}
Thanks to Ragu Swaminathan's answer, it works perfectly to exclude specific application in an IntentChooser. However, because the 'shareIntentLists' is added in front of the 'chooserIntent', some empty spaces may occur if the size of list is not multiple of 4. To solve this, try:
Intent chooserIntent = Intent.createChooser(new Intent(), "Choose app to share");