Suparv Sharma
Suparv Sharma

Reputation: 153

Intent.EXTRA_INITIAL_INTENTS does not add apps in chooser

I am trying to share some data from my app. I have to send different text in case of Email and different text in case user chooses other app.

                Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("ye");
                PackageManager pm = getPackageManager();
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");


                Intent openInChooser = Intent.createChooser(emailIntent, "Share via");



                List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
                List<LabeledIntent> intentList = new ArrayList<>();

                for(int i=0;i<resInfo.size();i++)
                {
                    ResolveInfo ri = resInfo.get(i);
                    String packageName = ri.activityInfo.packageName;
                    Log.d("package", i + " " + packageName);
                    if(packageName.contains("android.email")){
                        emailIntent.setPackage(packageName);
                        emailIntent.putExtra(Intent.EXTRA_TEXT, "This is email");
                    }
                    else
                    {
                        Intent intent = new Intent();

                        intent.setComponent(new ComponentName(packageName,ri.activityInfo.packageName));
                        intent.setAction(Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_TEXT, "Sharing via other app");
                        intent.setPackage(packageName);
                        intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
                    }
                }


                LabeledIntent [] extraIntents  = new LabeledIntent[intentList.size()];
                extraIntents = intentList.toArray(extraIntents);

                openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
                startActivity(openInChooser);

I have given a random string in my original intent in setType so that it displays only the chosen apps.However when I click on share an empty window comes up. I have checked with debugger and my extraIntents contains 24 intents. Its after startActivity that nothing comes up in my choosing options.

Upvotes: 2

Views: 4746

Answers (1)

Dhunju_likes_to_Learn
Dhunju_likes_to_Learn

Reputation: 1396

Update (Sept 9, 2020):

It seems the intents passed with EXTRA_INITIAL_INTENTS does not work the same on Android OS 10 and above. On the share sheet, it shows a new section with text, "Direct share not available", and share sheet does not show apps like WhatsApp that supports direct share.


Original Answer

In your case, emailIntent returns 0 apps because of random Type you have set. In

Intent openInChooser = Intent.createChooser(emailIntent, "Share via");

if the intent you pass returns 0 apps then it ignores EXTRA_INITIAL_INTENTS flag.

Possible solution,

            ......
            extraIntents = intentList.toArray(extraIntents);
            Intent firstIntent = extraIntents.remove(0); // assuming you will have at least one Intent
            Intent openInChooser = Intent.createChooser(firstIntent, "Share via");
          
            openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
            startActivity(openInChooser);

Upvotes: 7

Related Questions