2

I've found this answer, which explains how to create a different intent for each app, but some why it does not work:

here is the code that reproduces the issue, you can even comment the if and you will stick get the text and not the twitter

public Intent getShareChooserIntent(Context context) {
    Intent basicIntent = new Intent();
    basicIntent.setAction(Intent.ACTION_SEND);
    basicIntent.setType("text/plain");
    basicIntent.putExtra(Intent.EXTRA_TEXT, "text");
    basicIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
    PackageManager pm = context.getPackageManager();

    Intent openInChooser = Intent.createChooser(basicIntent, "share");
    List<ResolveInfo> resInfo = pm.queryIntentActivities(basicIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();        
    for (int i = 0; i < resInfo.size(); i++) {
       ResolveInfo ri = resInfo.get(i);
       String packageName = ri.activityInfo.packageName;
       Intent realIntent = new Intent();
       realIntent.setAction(Intent.ACTION_SEND);
       realIntent.setType("text/plain");
       realIntent.putExtra(Intent.EXTRA_TEXT, "text");
       realIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");

       if (packageName.contains("twitter")) // !!!!THIS LINE!!!!
           realIntent.putExtra(Intent.EXTRA_TEXT, "twitter");

       intentList.add(new LabeledIntent(realIntent, packageName, ri.loadLabel(pm), ri.icon));
    }

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray( new LabeledIntent[ intentList.size()]));
    return openInChooser;
}

what am I doing wrong?

Community
  • 1
  • 1
Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67

1 Answers1

4

Not sure what getShareBasicIntent do, but this code works fine for me (checked it with and without twitter)

static public Intent getShareChooserIntent(Context context) {

    PackageManager pm = context.getPackageManager();

    // Note that this intent should only be for email
    // to avoid duplicates in the list
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "not twitter"); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    emailIntent.setType("message/rfc822");
    Intent openInChooser = Intent.createChooser(emailIntent, "share");

    // the basic intent used to query all the activities that can support
    // text sharing
    Intent basicIntent = new Intent();
    basicIntent.setAction(Intent.ACTION_SEND);
    basicIntent.setType("text/plain"); 
    List<ResolveInfo> resInfo = pm.queryIntentActivities(basicIntent, 0);

    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();        
    for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        Intent intent=new Intent();
        intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, "not twitter"); 
        intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        intent.setPackage(packageName);

        if (packageName.contains("twitter")) { 
            intent.putExtra(Intent.EXTRA_TEXT, "twitter");
        }

        intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
    }

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray( new LabeledIntent[ intentList.size()]));
    return openInChooser;
}   
Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
Nir Hartmann
  • 1,389
  • 12
  • 14
  • Ohh I see my problem now, I've forgotten the `setComponent` and `setPackage` which are critical here. Note that your answer creates a duplicate list, one all the apps that match the basic intent, and the second the Intents you create in the list, to avoid such behavior, You can pass to the `Intent.createChooser` and Email only intent that will create the first list, and then add your custom intents later. Will be approved when fixed – Kirill Kulakov Nov 10 '14 at 17:35