blob: 8c5f862164b59ee400edba2fce6d709a3ab77b63 [file] [log] [blame]
Joe Malin45edbb72013-08-26 14:55:54 -07001page.title=Receiving Simple Data from Other Apps
2parent.title=Sharing Simple Data
Scott Main70645e32011-12-13 16:06:16 -08003parent.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>
Scott Main50e990c2012-06-21 17:14:39 -070026 <li><a href="{@docRoot}guide/components/intents-filters.html">Intents and
Scott Main70645e32011-12-13 16:06:16 -080027Intent Filters</a></li>
28</ul>
29
30</div>
31</div>
32
Joe Malin45edbb72013-08-26 14:55:54 -070033<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
Scott Mainf90f4ed2012-04-20 11:53:32 -070037<a href="https://play.google.com/store/apps/details?id=com.google.android.apps.plus">Google+ Android
Joe Malin45edbb72013-08-26 14:55:54 -070038application</a>
39accepts both text <em>and</em> single or multiple images. With this app, a user can easily start a
Scott Main70645e32011-12-13 16:06:16 -080040new Google+ post with photos from the Android Gallery app.</p>
41
42
43<h2 id="update-manifest">Update Your Manifest</h2>
44
Joe Malin45edbb72013-08-26 14:55:54 -070045<p>Intent filters inform the system what intents an application component is willing to accept.
46Similar to how you constructed an intent with action {@link android.content.Intent#ACTION_SEND} in
47the <a href="{@docRoot}training/sharing/send.html">Sending Simple Data to Other Apps</a>
48lesson, 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
Scott Main70645e32011-12-13 16:06:16 -080050<code><a
Joe Malin45edbb72013-08-26 14:55:54 -070051href="{@docRoot}guide/components/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
Scott Main70645e32011-12-13 16:06:16 -080053type, 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
Joe Malin45edbb72013-08-26 14:55:54 -070075<p class="note"><strong>Note:</strong> For more information on intent filters and intent resolution
Scott Main50e990c2012-06-21 17:14:39 -070076please read <a href="{@docRoot}guide/components/intents-filters.html#ifs">Intents and Intent
Scott Main70645e32011-12-13 16:06:16 -080077Filters</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
Joe Malin45edbb72013-08-26 14:55:54 -070081application 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
Adam Koch909fe932011-12-15 15:54:52 -050083is 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
Joe Malin45edbb72013-08-26 14:55:54 -070089android.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
Scott Main70645e32011-12-13 16:06:16 -080093examining 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
Joe Malin45edbb72013-08-26 14:55:54 -0700146<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
Scott Main70645e32011-12-13 16:06:16 -0800148to your application what happens next.</p>
149