I was wondering how Android applications and Android implements the share button and share mechanism. For instance, when I hit the share button on YouTube, a list off apps to share to pops up. How is this list determined? Is there a specific "share" intent, and then all spots capable of sharing the type of intent provided in the intent extras show up? What is the technical name of the design pattern used to implement this? Thanks.
-
1Did you really mean "poops", or is that a typo for "pops"? In American English at least, the former is what babies do to their diapers.– JDługoszJun 2, 2016 at 2:12
-
You're right. I recently switched to the Swype keyboard on Android which lets me type faster, but occasionally it enters the wrong word for and I don't notice. Not sure what to do about it yet, I'm not a very careful person. Sometimes in in the sub and cannot even see the screen properly.– Jack MaddingtonJun 2, 2016 at 6:42
-
In the sub (submarine?)! What to do: proofread before submitting. And don't pollute your prediction list with embarrassing words.– JDługoszJun 2, 2016 at 12:39
-
Coincidentally my prediction list is filled with stuff from old emails, which Swype read automatically. Yikes!– Jack MaddingtonJun 2, 2016 at 12:45
2 Answers
There are two parts to this: the mechanism for the communication apps to say they can share things, and the mechanism for the apps with content to share. I'll deal with them one at a time, but first a quick note about intents.
Intents
The intent in Android is what's used to start an app. It can work like a shortcut, or like a Windows file association. The intent can specify the name of the app to start, but it doesn't have to. It can also specify the action to perform (e.g. "open your main activity", "view this URL", "create a calendar entry") and the MIME type of the object it will act on (e.g. that the thing you want to view has type image/jpeg). Some names of actions are specific to a particular app, but many are standardised by Android to allow apps to interact.
Intent filters and communication app
Each app specifies intent filters to say what kind of actions it can perform. e.g. an image viewer app would register an intent filter for "view this URL" where the MIME type is "image/*"; a web browser would register an intent filter for "view this URL" where the URL scheme is "http" or "https". To be able to share things, an app registers an intent filter for the "send" intent, either with a specific type (e.g. if it can only share photos), or for any type.
Content app
The app that wants to share some content creates an intent with the "share" action and a URL pointing to the content to share. (This is often a "temporary" URL that gives the receiving app temporary access to the file in the content app's private storage, or to a stream generated by the content app.) It doesn't specify the name of an app to handle the intent. Then, it gives that intent to the Android framework and asks Android to display the chooser dialog. Android compares the intent with the intent filters from all installed apps, and creates the list and shows it to the user. When the user picks a particular app, Android starts that app and passes it the intent. The app uses the URL to get the content from the original app.
New in Marshmallow: Direct Share
In Marshmallow, there's another mechanism called Direct Share. This supplements the intent mechanism so that you can choose people or groups to share to directly from the chooser dialog, skipping the extra step of "who would you like to send it to". This works slightly different: the communication app has to provide a service. When the app with the content asks for a chooser, Android starts the service for each relevant app. The service returns a list of relevant share targets, and Android assembles all those separate lists into the list that gets shown in the dialog.
-
But, I've seen apps show quite a few different chooser dialogs. Are they all really displayed by the OS, or can the app itself get a list of targets?– user1686Jun 2, 2016 at 5:40
-
@grawity The built-in dialog has some customization options (e.g. to set the title), but it's also possible for the app to ask Android for the list of share targets and put them into its own dialog. My browser chooser bURLy is an example of this, but with the "open URL" intent instead of the "send" intent. Jun 2, 2016 at 8:21
I'm not a dev, so I just can give a raw explanation: apps declare in their Manifest
which Mimetypes they understand and willing to respond to on share actions. For this they define so called "intent filters", e.g.:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/*" />
<data android:mimeType="audio/*" />
<data android:mimeType="image/*" />
<data android:mimeType="message/*" />
<data android:mimeType="multipart/*" />
<data android:mimeType="text/*" />
<data android:mimeType="video/*" />
</intent-filter>
So whenever you hit the "share" button, the content-to-share is evaluated and matched against apps having declared their willingness to receive the format.
Sources:
- Intent Filter to capture all sharing Intents (StackOverflow)
- Receiving Simple Data from Other Apps (developer.android.com)
-
1That's pretty much it directly. The app (doing the sharing) it's self can filter some of the intent respondents, but most do not.– coteyrJun 1, 2016 at 15:38
-
2@HiltonShumway that's why I upvoted his answer. Other than me, Dan is fit in development and knows much better how things go together. So when I posted my answer, I was hoping he would post one as well – he didn't disappoint my hope :)– Izzy ♦Jun 1, 2016 at 16:32