1
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfo.isEmpty()) {
    for (ResolveInfo resolveInfo : resInfo) {
        String packageName = resolveInfo.activityInfo.packageName;
        Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
        targetedShareIntent.setType("text/plain");
        targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject to be shared");
        if (TextUtils.equals(packageName, "com.facebook.katana")) {
            targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "http://link-to-be-shared.com");
        } else {
            targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text message to shared");
        }
        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);
}

In order to visualize the desired apps in share via, I need to pass the:

 Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");

Why do I need to have: targetedShareIntents.remove(0) Does this mean an intent is removed from targetedShareIntents?

I've seen this code used a lot. I don't understand why do we need to use remove. Thx

justmee
  • 355
  • 2
  • 14

1 Answers1

1

targetedShareIntents.remove(0) means throwing the list after removing the first element... because the first targetedShareIntent package name is com.google.android.apps.docs so the documentation app is unnecessary .. so removing the unnecessary app is better..because the user doesnt want to send data to it... Thats the reason of removing that targetedShareIntent from the list... You can log and check the package name ...

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);

    Log.e("package",resInfo.get(0).activityInfo.packageName);
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • thank you for your comment. What if I don t want to remove anything from the targetedShareIntents what should I use? I know I ve tried to add an intent with only the action_send as type, but on Android 5 is not working. I don t see anything in the share via dialog. – justmee Feb 12 '18 at 13:26
  • i dint get you.. can you explain your question properly after editing the question.. – Santanu Sur Feb 12 '18 at 13:27
  • if you dont wanna remove anything from the list you can use... `Intent chooserIntent = Intent.createChooser(targetedShareIntents, "Select app to share");` and if you got your answer please update... – Santanu Sur Feb 12 '18 at 13:34
  • I cannot use Intent chooserIntent = Intent.createChooser(targetedShareIntents, "Select app to share"); since targetShareIntents is a List – justmee Feb 12 '18 at 23:13