overridefunonCreate(savedInstanceState:Bundle?){...when{intent?.action==Intent.ACTION_SEND->{if("text/plain"==intent.type){handleSendText(intent)// Handle text being sent}elseif(intent.type?.startsWith("image/")==true){handleSendImage(intent)// Handle single image being sent}}intent?.action==Intent.ACTION_SEND_MULTIPLE && intent.type?.startsWith("image/")==true->{handleSendMultipleImages(intent)// Handle multiple images being sent}else->{// Handle other intents, such as being started from the home screen}}...}privatefunhandleSendText(intent:Intent){intent.getStringExtra(Intent.EXTRA_TEXT)?.let{// Update UI to reflect text being shared}}privatefunhandleSendImage(intent:Intent){(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM)as?Uri)?.let{// Update UI to reflect image being shared}}privatefunhandleSendMultipleImages(intent:Intent){intent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.let{// Update UI to reflect multiple images being shared}}
Java
voidonCreate(BundlesavedInstanceState){...// Get intent, action and MIME typeIntentintent=getIntent();Stringaction=intent.getAction();Stringtype=intent.getType();if(Intent.ACTION_SEND.equals(action) && type!=null){if("text/plain".equals(type)){handleSendText(intent);// Handle text being sent}elseif(type.startsWith("image/")){handleSendImage(intent);// Handle single image being sent}}elseif(Intent.ACTION_SEND_MULTIPLE.equals(action) && type!=null){if(type.startsWith("image/")){handleSendMultipleImages(intent);// Handle multiple images being sent}}else{// Handle other intents, such as being started from the home screen}...}voidhandleSendText(Intentintent){StringsharedText=intent.getStringExtra(Intent.EXTRA_TEXT);if(sharedText!=null){// Update UI to reflect text being shared}}voidhandleSendImage(Intentintent){UriimageUri=(Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);if(imageUri!=null){// Update UI to reflect image being shared}}voidhandleSendMultipleImages(Intentintent){ArrayList<Uri>imageUris=intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);if(imageUris!=null){// Update UI to reflect multiple images being shared}}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["Just as an app can send data to other apps, it can also receive data from other\napps as well. Think about how users interact with your application and what data\ntypes you want to receive from other applications. For example, a social\nnetworking application might be interested in receiving text content, like an\ninteresting web URL, from another app.\n\nUsers of other apps frequently send data to your app through the Android\nSharesheet or the intent resolver. Apps that send data to your app must set a\nMIME type for that data. Your app can receive data sent by another app in the\nfollowing ways:\n\n- An `Activity` with a matching `intent-filter` tag in the manifest\n- Sharing Shortcuts published by your app.\n\nDirect Share targets are deep links into a specific Activity within your app.\nThey often represent a person or a group, and the Android Sharesheet shows them.\nFor example, a messaging app can provide a Direct Share target for a person that\ndeep links directly into a conversation with that person. See\n[Provide Direct Share targets](/training/sharing/direct-share-targets) for detailed\ninstructions.\n\nSupport MIME types\n\nIdeally, an app must be able to receive the widest possible range of MIME types.\nFor example, a messaging app designed for sending text, images, and video\nideally supports receiving `text/*`, `image/*` and `video/*`. Here are a few\ncommon MIME types for sending and receiving simple data in Android.\n\n| Receivers register for | Senders send |\n|---------------------------|---------------------------------------------------------|\n| `text/*` | - `text/plain` - `text/rtf` - `text/html` - `text/json` |\n| ```image/*``` | - `image/jpg` - `image/png` - `image/gif` |\n| `video/*` | - `video/mp4` - `video/3gp` |\n| Supported file extensions | `application/pdf` |\n\nRefer to the [IANA](https://www.iana.org/assignments/media-types/media-types.xhtml) official registry of MIME media types.\n| **Caution:** Although you can receive a MIME type of `*/*`, we strongly discourage doing so unless your app is fully capable of handling any type of incoming content.\n\nMake great share targets\n\nWhen a user taps on a share target associated with a specific activity they\nshould be able to confirm and edit the shared content before using it. This is\nespecially important for text data.\n\nReceive data with an activity\n\nReceiving data with an activity involves updating your manifest, handling the\nincoming content, and ensuring that the user recognizes your app.\n\nUpdate your manifest\n\nIntent filters inform the system which intents an app component accepts.\nSimilar to how you constructed an intent with an `ACTION_SEND` action in the\n[Sending simple data to other apps](/training/sharing/send)\nlesson, you create intent filters to receive intents with this action. You\ndefine an intent filter in your manifest using the `\u003cintent-filter\u003e` element.\nFor example, if your app handles receiving text content, a manifest that\nincludes one or more images of any type would look like the following snippet: \n\n```xml\n\u003cactivity android:name=\".ui.MyActivity\" \u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.intent.action.SEND\" /\u003e\n \u003ccategory android:name=\"android.intent.category.DEFAULT\" /\u003e\n \u003cdata android:mimeType=\"image/*\" /\u003e\n \u003c/intent-filter\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.intent.action.SEND\" /\u003e\n \u003ccategory android:name=\"android.intent.category.DEFAULT\" /\u003e\n \u003cdata android:mimeType=\"text/plain\" /\u003e\n \u003c/intent-filter\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.intent.action.SEND_MULTIPLE\" /\u003e\n \u003ccategory android:name=\"android.intent.category.DEFAULT\" /\u003e\n \u003cdata android:mimeType=\"image/*\" /\u003e\n \u003c/intent-filter\u003e\n\u003c/activity\u003e\n```\n\nWhen another app tries to share any of these things by constructing an\nintent and passing it to [`startActivity()`](/reference/android/content/Context#startActivity(android.content.Intent)), your application\nis listed as an option in the Android Sharesheet or intent resolver. If the user\nselects your app, this starts the corresponding activity (`.ui.MyActivity` in\nthe preceding example). It's then up to you to handle the content appropriately\nwithin your code and UI.\n| **Note:** For more information about intent filters and intent resolution, read [Intents and Intent Filters](/guide/components/intents-filters#ifs).\n\nHandle the incoming content\n\nTo handle the content delivered by an [`Intent`](/reference/android/content/Intent), call\n[`getIntent()`](/reference/android/content/Intent#getIntent(java.lang.String)) to get the `Intent` object. Once you have the object,\nyou can examine its contents to determine what to do next. If this activity can\nbe started from other parts of the system (such as the launcher), take this\ninto consideration when examining the intent.\n\nTake extra care to check the incoming data, you never know what some other\napplication may send you. For example, the wrong MIME type might be set, or the\nimage being sent might be extremely large. Also, remember to process binary data\nin a separate thread rather than the main (\"UI\") thread. \n\nKotlin \n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n ...\n when {\n intent?.action == Intent.ACTION_SEND -\u003e {\n if (\"text/plain\" == intent.type) {\n handleSendText(intent) // Handle text being sent\n } else if (intent.type?.startsWith(\"image/\") == true) {\n handleSendImage(intent) // Handle single image being sent\n }\n }\n intent?.action == Intent.ACTION_SEND_MULTIPLE\n && intent.type?.startsWith(\"image/\") == true -\u003e {\n handleSendMultipleImages(intent) // Handle multiple images being sent\n }\n else -\u003e {\n // Handle other intents, such as being started from the home screen\n }\n }\n ...\n}\n\nprivate fun handleSendText(intent: Intent) {\n intent.getStringExtra(Intent.EXTRA_TEXT)?.let {\n // Update UI to reflect text being shared\n }\n}\n\nprivate fun handleSendImage(intent: Intent) {\n (intent.getParcelableExtra\u003cParcelable\u003e(Intent.EXTRA_STREAM) as? Uri)?.let {\n // Update UI to reflect image being shared\n }\n}\n\nprivate fun handleSendMultipleImages(intent: Intent) {\n intent.getParcelableArrayListExtra\u003cParcelable\u003e(Intent.EXTRA_STREAM)?.let {\n // Update UI to reflect multiple images being shared\n }\n}\n```\n\nJava \n\n```java\nvoid onCreate (Bundle savedInstanceState) {\n ...\n // Get intent, action and MIME type\n Intent intent = getIntent();\n String action = intent.getAction();\n String type = intent.getType();\n\n if (Intent.ACTION_SEND.equals(action) && type != null) {\n if (\"text/plain\".equals(type)) {\n handleSendText(intent); // Handle text being sent\n } else if (type.startsWith(\"image/\")) {\n handleSendImage(intent); // Handle single image being sent\n }\n } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {\n if (type.startsWith(\"image/\")) {\n handleSendMultipleImages(intent); // Handle multiple images being sent\n }\n } else {\n // Handle other intents, such as being started from the home screen\n }\n ...\n}\n\nvoid handleSendText(Intent intent) {\n String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);\n if (sharedText != null) {\n // Update UI to reflect text being shared\n }\n}\n\nvoid handleSendImage(Intent intent) {\n Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);\n if (imageUri != null) {\n // Update UI to reflect image being shared\n }\n}\n\nvoid handleSendMultipleImages(Intent intent) {\n ArrayList\u003cUri\u003e imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);\n if (imageUris != null) {\n // Update UI to reflect multiple images being shared\n }\n}\n```\n\nUpdating the UI after receiving the data can be as simple as populating an\n[`EditText`](/reference/android/widget/EditText), or it can be more\ncomplicated like applying an interesting photo filter to an image. It's up to\nyour app what happens next.\n\nEnsure users recognize your app\n\nYour app is represented by its\n[icon](/guide/topics/manifest/application-element#icon) and\n[label](/guide/topics/manifest/application-element#label) in the Android\nSharesheet and intent resolver. These are both defined in the manifest. You can\nset activity or intent filter labels to provide more context.\n\nAs of Android 10 (API level 29), the Android Sharesheet only uses icons set in\nthe manifest on your `application` tag. Android ignores icons set on\n`intent-filter` and `activity` tags.\n| **Note:** Effective share targets don't need a label and icon in the associated activity or intent filter. The receiving app's name and icon alone must be enough for a user to understand what happens when they share."]]