Scott Main | 70645e3 | 2011-12-13 16:06:16 -0800 | [diff] [blame] | 1 | page.title=Receiving Content from Other Apps |
| 2 | parent.title=Sharing Content |
| 3 | parent.link=index.html |
| 4 | |
| 5 | trainingnavtop=true |
| 6 | previous.title=Sending Content to Other Apps |
| 7 | previous.link=send.html |
| 8 | next.title=Adding an Easy Share Action |
| 9 | next.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 |
| 27 | Intent 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 |
| 34 | from applications. Think about how users interact with your application, and what data types you |
| 35 | want to receive from other applications. For example, a social networking application would likely |
| 36 | be 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 |
| 38 | application</a> |
| 39 | accepts both text <em>and</em> single or multiple images. With this app, a user can easily start a |
| 40 | new 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 Koch | 909fe93 | 2011-12-15 15:54:52 -0500 | [diff] [blame] | 46 | Similar to how you constructed an intent with action {@link android.content.Intent#ACTION_SEND} in |
| 47 | the <a href="{@docRoot}training/sharing/send.html">Send Content to Other Apps Using Intents</a> |
Scott Main | 70645e3 | 2011-12-13 16:06:16 -0800 | [diff] [blame] | 48 | lesson, you create intent filters in order to be able to receive intents with this action. You |
| 49 | define an intent filter in your manifest, using the |
| 50 | <code><a |
| 51 | href="{@docRoot}guide/topics/intents/intents-filters.html#ifs"><intent-filter></a></code> |
| 52 | element. For example, if your application handles receiving text content, a single image of any |
| 53 | type, or multiple images of any type, your manifest would look like:</p> |
| 54 | |
| 55 | <pre> |
| 56 | <activity android:name=".ui.MyActivity" > |
| 57 | <intent-filter> |
| 58 | <action android:name="android.intent.action.SEND" /> |
| 59 | <category android:name="android.intent.category.DEFAULT" /> |
| 60 | <data android:mimeType="image/*" /> |
| 61 | </intent-filter> |
| 62 | <intent-filter> |
| 63 | <action android:name="android.intent.action.SEND" /> |
| 64 | <category android:name="android.intent.category.DEFAULT" /> |
| 65 | <data android:mimeType="text/plain" /> |
| 66 | </intent-filter> |
| 67 | <intent-filter> |
| 68 | <action android:name="android.intent.action.SEND_MULTIPLE" /> |
| 69 | <category android:name="android.intent.category.DEFAULT" /> |
| 70 | <data android:mimeType="image/*" /> |
| 71 | </intent-filter> |
| 72 | </activity> |
| 73 | </pre> |
| 74 | |
| 75 | <p class="note"><strong>Note:</strong> For more information on intent filters and intent resolution |
| 76 | please read <a href="{@docRoot}guide/topics/intents/intents-filters.html#ifs">Intents and Intent |
| 77 | Filters</a></p> |
| 78 | |
| 79 | <p>When another application tries to share any of these things by constructing an intent and passing |
| 80 | it to {@link android.content.Context#startActivity(android.content.Intent) startActivity()}, your |
Adam Koch | 909fe93 | 2011-12-15 15:54:52 -0500 | [diff] [blame] | 81 | application will be listed as an option in the intent chooser. If the user selects your application, |
| 82 | the corresponding activity (<code>.ui.MyActivity</code> in the example above) will be started. It |
| 83 | is then up to you to handle the content appropriately within your code and UI.</p> |
Scott Main | 70645e3 | 2011-12-13 16:06:16 -0800 | [diff] [blame] | 84 | |
| 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 |
| 89 | android.content.Intent#getIntent(String) getIntent()} |
| 90 | to get {@link android.content.Intent} object. Once you have the object, you can examine its |
| 91 | contents to determine what to do next. Keep in mind that if this activity can be started from other |
| 92 | parts of the system, such as the launcher, then you will need to take this into consideration when |
| 93 | examining the intent.</p> |
| 94 | |
| 95 | <pre> |
| 96 | void 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) && type != null) { |
| 104 | if ("text/plain".equals(type)) { |
| 105 | handleSendText(intent); // Handle text being sent |
| 106 | } else if (type.startsWith("image/")) { |
| 107 | handleSendImage(intent); // Handle single image being sent |
| 108 | } |
| 109 | } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) { |
| 110 | if (type.startsWith("image/")) { |
| 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 | |
| 119 | void 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 | |
| 126 | void 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 | |
| 133 | void handleSendMultipleImages(Intent intent) { |
| 134 | ArrayList<Uri> 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 |
| 142 | know what some other application may send you. For example, the wrong MIME type might be set, or the |
| 143 | image being sent might be extremely large. Also, remember to process binary data in a separate |
| 144 | thread 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 |
| 147 | be more complicated like applying an interesting photo filter to an image. It's really specific |
| 148 | to your application what happens next.</p> |
| 149 | |