2

I would like to share a URI from my app, and have the app chooser dialog show options for ACTION_SEND apps (like SMS and copy to clipboard) as well as ACTION_VIEW apps (like Chrome). So far, I can only seem to get one set of apps to show at a time. Is there a way to combine intent actions?

Here is what plain ACTION_SEND intent looks like:

Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "www.example.com");
context.startActivity(Intent.createChooser(i, "Share"));

This results in the normal chooser for apps that send information. But no open in browser options.

Here is what ACTION_VIEW intent looks like:

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.putData(Uri.parse("www.example.com"))
context.startActivity(Intent.createChooser(i, "Share"));

This results in the normal chooser for opening the link in the browser. But no options for info sending apps.

Is there a way to "combine" these two behaviors so both sets of options show up in the chooser dialog?

I have also tried to add categories to the intent, but no luck.

EDIT: I stumbled across this question where the OP has the same issue. However, I would like a solution that does not involve creating a bunch of custom activities for each app I'd like to show in the chooser.

Chase Farmer
  • 113
  • 9

2 Answers2

2

I know this is late, but have a different solution: Create intents for send and view. Create a chooser intent for one of them and pass the other intent as Intent.EXTRA_INITIAL_INTENTS. Like this:

// Share
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.setDataAndType(uri, MIME_PDF_TYPE)
sendIntent.putExtra(Intent.EXTRA_TEXT, "TEST")
sendIntent.putExtra(Intent.EXTRA_STREA, uri)
    
// Open
val openIntent = Intent(Intent.ACTION_VIEW)
openIntent.setDataAndType(uri, MIME_PDF_TYPE)
openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    
    
val chooserIntent = Intent.createChooser(sendIntent,activity.getString(R.string.sharing_title))
    
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(openIntent))
    
activity.startActivity(chooserIntent)
-1

i have kotlin snippet and i'm sharing with you. You can use sharecompact builder.

ShareCompat.IntentBuilder.from(requireActivity())
                    .setType("text/plain")
                    .setSubject(getString(R.string.app_name))
                    .setChooserTitle("Share via")
                    .setText(your text)
                    .startChooser()
MEHUL
  • 39
  • 5
  • I experimented with `ShareCompat` a bit, but nothing was able to get the browser options to show. Is there a specific function that tells Android that browsers can open the text? – Chase Farmer Aug 19 '19 at 20:32