blob: cc55967a11e0b34d57e1bf108fb3e19c9659599a [file] [log] [blame]
Scott Main70645e32011-12-13 16:06:16 -08001page.title=Receiving Content from Other Apps
2parent.title=Sharing Content
3parent.link=index.html
4
5trainingnavtop=true
6previous.title=Sending Content to Other Apps
7previous.link=send.html
8next.title=Adding an Easy Share Action
9next.link=shareaction.html
10
11@jd:body
12
13<div id="tb-wrapper">
14<div id="tb">
15
16<!-- table of contents -->
17<h2>This lesson teaches you to</h2>
18<ol>
19 <li><a href="#update-manifest">Update Your Manifest</a></li>
20 <li><a href="#handling-content">Handle the Incoming Content</a></li>
21</ol>
22
23<!-- other docs (NOT javadocs) -->
24<h2>You should also read</h2>
25<ul>
26 <li><a href="{@docRoot}guide/topics/intents/intents-filters.html">Intents and
27Intent Filters</a></li>
28</ul>
29
30</div>
31</div>
32
33<p>Just as your application can send data to other applications, so too can it easily receive data
34from applications. Think about how users interact with your application, and what data types you
35want to receive from other applications. For example, a social networking application would likely
36be interested in receiving text content, like an interesting web URL, from another app. The
37<a href="https://market.android.com/details?id=com.google.android.apps.plus">Google+ Android
38application</a>
39accepts both text <em>and</em> single or multiple images. With this app, a user can easily start a
40new Google+ post with photos from the Android Gallery app.</p>
41
42
43<h2 id="update-manifest">Update Your Manifest</h2>
44
45<p>Intent filters inform the system what intents an application component is willing to accept.
Adam Koch909fe932011-12-15 15:54:52 -050046Similar to how you constructed an intent with action {@link android.content.Intent#ACTION_SEND} in
47the <a href="{@docRoot}training/sharing/send.html">Send Content to Other Apps Using Intents</a>
Scott Main70645e32011-12-13 16:06:16 -080048lesson, you create intent filters in order to be able to receive intents with this action. You
49define an intent filter in your manifest, using the
50<code><a
51href="{@docRoot}guide/topics/intents/intents-filters.html#ifs">&lt;intent-filter&gt;</a></code>
52element. For example, if your application handles receiving text content, a single image of any
53type, or multiple images of any type, your manifest would look like:</p>
54
55<pre>
56&lt;activity android:name=&quot;.ui.MyActivity&quot; &gt;
57 &lt;intent-filter&gt;
58 &lt;action android:name=&quot;android.intent.action.SEND&quot; /&gt;
59 &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
60 &lt;data android:mimeType=&quot;image/*&quot; /&gt;
61 &lt;/intent-filter&gt;
62 &lt;intent-filter&gt;
63 &lt;action android:name=&quot;android.intent.action.SEND&quot; /&gt;
64 &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
65 &lt;data android:mimeType=&quot;text/plain&quot; /&gt;
66 &lt;/intent-filter&gt;
67 &lt;intent-filter&gt;
68 &lt;action android:name=&quot;android.intent.action.SEND_MULTIPLE&quot; /&gt;
69 &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
70 &lt;data android:mimeType=&quot;image/*&quot; /&gt;
71 &lt;/intent-filter&gt;
72&lt;/activity&gt;
73</pre>
74
75<p class="note"><strong>Note:</strong> For more information on intent filters and intent resolution
76please read <a href="{@docRoot}guide/topics/intents/intents-filters.html#ifs">Intents and Intent
77Filters</a></p>
78
79<p>When another application tries to share any of these things by constructing an intent and passing
80it to {@link android.content.Context#startActivity(android.content.Intent) startActivity()}, your
Adam Koch909fe932011-12-15 15:54:52 -050081application will be listed as an option in the intent chooser. If the user selects your application,
82the corresponding activity (<code>.ui.MyActivity</code> in the example above) will be started. It
83is then up to you to handle the content appropriately within your code and UI.</p>
Scott Main70645e32011-12-13 16:06:16 -080084
85
86<h2 id="handling-content">Handle the Incoming Content</h2>
87
88<p>To handle the content delivered by an {@link android.content.Intent}, start by calling {@link
89android.content.Intent#getIntent(String) getIntent()}
90to get {@link android.content.Intent} object. Once you have the object, you can examine its
91contents to determine what to do next. Keep in mind that if this activity can be started from other
92parts of the system, such as the launcher, then you will need to take this into consideration when
93examining the intent.</p>
94
95<pre>
96void onCreate (Bundle savedInstanceState) {
97 ...
98 // Get intent, action and MIME type
99 Intent intent = getIntent();
100 String action = intent.getAction();
101 String type = intent.getType();
102
103 if (Intent.ACTION_SEND.equals(action) &amp;&amp; type != null) {
104 if (&quot;text/plain&quot;.equals(type)) {
105 handleSendText(intent); // Handle text being sent
106 } else if (type.startsWith(&quot;image/&quot;)) {
107 handleSendImage(intent); // Handle single image being sent
108 }
109 } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) &amp;&amp; type != null) {
110 if (type.startsWith(&quot;image/&quot;)) {
111 handleSendMultipleImages(intent); // Handle multiple images being sent
112 }
113 } else {
114 // Handle other intents, such as being started from the home screen
115 }
116 ...
117}
118
119void handleSendText(Intent intent) {
120 String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
121 if (sharedText != null) {
122 // Update UI to reflect text being shared
123 }
124}
125
126void handleSendImage(Intent intent) {
127 Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
128 if (imageUri != null) {
129 // Update UI to reflect image being shared
130 }
131}
132
133void handleSendMultipleImages(Intent intent) {
134 ArrayList&lt;Uri&gt; imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
135 if (imageUris != null) {
136 // Update UI to reflect multiple images being shared
137 }
138}
139</pre>
140
141<p class="caution"><strong>Caution:</strong> Take extra care to check the incoming data, you never
142know what some other application may send you. For example, the wrong MIME type might be set, or the
143image being sent might be extremely large. Also, remember to process binary data in a separate
144thread rather than the main ("UI") thread.</p>
145
146<p>Updating the UI can be as simple as populating an {@link android.widget.EditText}, or it can
147be more complicated like applying an interesting photo filter to an image. It's really specific
148to your application what happens next.</p>
149