classMyReceiver:OnReceiveContentListener{overridefunonReceiveContent(view:View,contentInfo:ContentInfoCompat):ContentInfoCompat{valsplit=contentInfo.partition{item:ClipData.Item->item.uri!=null}valuriContent=split.firstvalremaining=split.secondif(uriContent!=null){// App-specific logic to handle the URI(s) in uriContent.}// Return anything that your app didn't handle. This preserves the// default platform behavior for text and anything else that you aren't// implementing custom handling for.returnremaining}companionobject{valMIME_TYPES=arrayOf("image/*","video/*")}}
Java
publicclassMyReceiverimplementsOnReceiveContentListener{publicstaticfinalString[]MIME_TYPES=newString[]{"image/*","video/*"};@OverridepublicContentInfoCompatonReceiveContent(Viewview,ContentInfoCompatcontentInfo){Pair<ContentInfoCompat,ContentInfoCompat>split=contentInfo.partition(item->item.getUri()!=null);ContentInfouriContent=split.first;ContentInforemaining=split.second;if(uriContent!=null){// App-specific logic to handle the URI(s) in uriContent.}// Return anything that your app didn't handle. This preserves the// default platform behavior for text and anything else that you aren't// implementing custom handling for.returnremaining;}}
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Receive rich content\n\n**Figure 1.** The unified API provides a single place to handle incoming content regardless of the specific UI mechanism, such as pasting from the touch \\& hold menu or using drag-and-drop.\n\nUsers love images, videos, and other expressive content, but inserting and\nmoving this content in apps isn't always easy. To make it simpler for apps to\nreceive rich content, Android 12 (API level 31) introduces a unified API that\nlets your app accept content from any source: clipboard, keyboard, or dragging.\n\nYou can attach an interface, such as\n[`OnReceiveContentListener`](/reference/android/view/OnReceiveContentListener),\nto UI components and get a callback when content is inserted through any\nmechanism. The callback becomes the single place for your code to handle\nreceiving all content, from plain and styled text to markup, images, videos,\naudio files, and others.\n\nFor backward compatibility with previous Android versions, this API is also\navailable in AndroidX, starting from\n[Core 1.7](/jetpack/androidx/releases/core#1.7.0) and\n[Appcompat 1.4](/jetpack/androidx/releases/appcompat#1.4.0),\nwhich we recommend you use when implementing this functionality.\n\nOverview\n--------\n\nWith other existing APIs, each UI mechanism---such as the touch \\& hold menu\nor dragging---has its own corresponding API. This means that you have to\nintegrate with each API separately, adding similar code for each mechanism that\ninserts content:\n**Figure 2.** Previously, apps implemented a different API for each UI mechanism for inserting content.\n\nThe `OnReceiveContentListener` API consolidates these different code paths by\ncreating a single API to implement, so you can focus on your app-specific logic\nand let the platform handle the rest:\n**Figure 3.** The unified API lets you implement a single API that supports all UI mechanisms.\n\nThis approach also means that when new ways of inserting content are added to\nthe platform, you don't need to make additional code changes to enable support\nin your app. And if your app needs to implement full customization for a\nparticular use case, you can still use the existing APIs, which continue to work\nthe same way.\n\nImplementation\n--------------\n\n| **Note:** See the [Drag and Drop sample](https://github.com/android/platform-samples/tree/main/samples/user-interface/draganddrop) for a complete implementation of [`DropHelper`](/reference/kotlin/androidx/draganddrop/DropHelper), which implements `OnReceiveContentListener`. For more details, see the guide on [Enabling drag and drop](/develop/ui/views/touch-and-input/drag-drop).\n\nThe API is a listener interface with a single method,\n[`OnReceiveContentListener`](/reference/android/view/OnReceiveContentListener).\nTo support older versions of the Android platform, we recommend using the\nmatching\n[`OnReceiveContentListener`](/reference/androidx/core/view/OnReceiveContentListener)\ninterface in the AndroidX Core library.\n\nTo use the API, implement the listener by specifying what types of content your\napp can handle: \n\n### Kotlin\n\n```kotlin\nobject MyReceiver : OnReceiveContentListener {\n val MIME_TYPES = arrayOf(\"image/*\", \"video/*\")\n \n // ...\n \n override fun onReceiveContent(view: View, payload: ContentInfoCompat): ContentInfoCompat? {\n TODO(\"Not yet implemented\")\n }\n}\n```\n\n### Java\n\n```java\npublic class MyReceiver implements OnReceiveContentListener {\n public static final String[] MIME_TYPES = new String[] {\"image/*\", \"video/*\"};\n // ...\n}\n```\n\nAfter specifying all the content MIME types that your app supports, implement\nthe rest of the listener: \n\n### Kotlin\n\n```kotlin\nclass MyReceiver : OnReceiveContentListener {\n override fun onReceiveContent(view: View, contentInfo: ContentInfoCompat): ContentInfoCompat {\n val split = contentInfo.partition { item: ClipData.Item -\u003e item.uri != null }\n val uriContent = split.first\n val remaining = split.second\n if (uriContent != null) {\n // App-specific logic to handle the URI(s) in uriContent.\n }\n // Return anything that your app didn't handle. This preserves the\n // default platform behavior for text and anything else that you aren't\n // implementing custom handling for.\n return remaining\n }\n\n companion object {\n val MIME_TYPES = arrayOf(\"image/*\", \"video/*\")\n }\n}\n```\n\n### Java\n\n```java\n public class MyReceiver implements OnReceiveContentListener {\n public static final String[] MIME_TYPES = new String[] {\"image/*\", \"video/*\"};\n\n @Override\n public ContentInfoCompat onReceiveContent(View view, ContentInfoCompat contentInfo) {\n Pair\u003cContentInfoCompat, ContentInfoCompat\u003e split = contentInfo.partition(\n item -\u003e item.getUri() != null);\n ContentInfo uriContent = split.first;\n ContentInfo remaining = split.second;\n if (uriContent != null) {\n // App-specific logic to handle the URI(s) in uriContent.\n }\n // Return anything that your app didn't handle. This preserves the\n // default platform behavior for text and anything else that you aren't\n // implementing custom handling for.\n return remaining;\n }\n }\n```\n\nIf your app already supports sharing with intents, you can reuse your\napp-specific logic for handling content URIs. Return any remaining data to\ndelegate handling of that data to the platform.\n\nAfter implementing the listener, set it on the appropriate UI elements in\nyour app: \n\n### Kotlin\n\n```kotlin\nclass MyActivity : Activity() {\n public override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n // ...\n val myInput = findViewById(R.id.my_input)\n ViewCompat.setOnReceiveContentListener(myInput, MyReceiver.MIME_TYPES, MyReceiver())\n }\n}\n```\n\n### Java\n\n```java\npublic class MyActivity extends Activity {\n @Override\n public void onCreate(Bundle savedInstanceState) {\n // ...\n\n AppCompatEditText myInput = findViewById(R.id.my_input);\n ViewCompat.setOnReceiveContentListener(myInput, MyReceiver.MIME_TYPES, new MyReceiver());\n }\n}\n```\n\nURI permissions\n---------------\n\nRead permissions are granted and released automatically by the platform for any\n[content URIs](/reference/android/content/ContentResolver#SCHEME_CONTENT) in the\npayload passed to the `OnReceiveContentListener`.\n\nNormally, your app processes content URIs in a service or activity. For\nlong-running processing, use\n[WorkManager](/topic/libraries/architecture/workmanager). When you implement\nthis, extend permissions to the target service or activity by passing the\ncontent using\n[`Intent.setClipData`](/reference/android/content/Intent#setClipData(android.content.ClipData))\nand [setting](/reference/android/content/Intent#addFlags(int)) the flag\n[`FLAG_GRANT_READ_URI_PERMISSION`](/reference/android/content/Intent#FLAG_GRANT_READ_URI_PERMISSION).\n\nAlternatively, you can use a background thread within the current context to\nprocess the content. In this case, you must maintain a reference to the\n`payload` object received by the listener to help ensure that permissions aren't\nrevoked prematurely by the platform.\n\nCustom views\n------------\n\nIf your app uses a custom `View` subclass, take care to ensure that the\n`OnReceiveContentListener` isn't bypassed.\n\nIf your `View` class overrides the\n[`onCreateInputConnection`](/reference/android/widget/TextView#onCreateInputConnection(android.view.inputmethod.EditorInfo))\nmethod, use the Jetpack API\n[`InputConnectionCompat.createWrapper`](/reference/androidx/core/view/inputmethod/InputConnectionCompat#createWrapper(android.view.View,%20android.view.inputmethod.InputConnection,%20android.view.inputmethod.EditorInfo))\nto configure the `InputConnection`.\n\nIf your `View` class overrides the\n[`onTextContextMenuItem`](/reference/android/widget/TextView#onTextContextMenuItem(int))\nmethod, delegate to super when the menu item is\n[`R.id.paste`](/reference/android/R.id#paste) or\n[`R.id.pasteAsPlainText`](/reference/android/R.id#pasteAsPlainText).\n\nComparison with the keyboard image API\n--------------------------------------\n\nYou can think of the `OnReceiveContentListener` API as the next version of the\nexisting [keyboard image API](/guide/topics/text/image-keyboard). This unified\nAPI supports the functionality of the keyboard image API as well as some\nadditional features. Device and feature compatibility varies depending on\nwhether you use the Jetpack library or the native APIs from the Android SDK.\n\n| Action or feature | Supported by keyboard image API | Supported by unified API |\n|------------------------------------------------|---------------------------------|-------------------------------|\n| Insert from the keyboard | Yes (API level 13 and higher) | Yes (API level 13 and higher) |\n| Insert using paste from the touch \\& hold menu | No | Yes |\n| Insert using drag-and-drop | No | Yes (API level 24 and higher) |\n[**Table 1.** Supported features and API levels for\nJetpack.]\n\n| Action or feature | Supported by keyboard image API | Supported by unified API |\n|------------------------------------------------|---------------------------------|-----------------------------|\n| Insert from the keyboard | Yes (API level 25 and higher) | Yes (Android 12 and higher) |\n| Insert using paste from the touch \\& hold menu | No | Yes (Android 12 and higher) |\n| Insert using drag and drop | No | Yes (Android 12 and higher) |\n[**Table 2.** Supported features and API levels for native\nAPIs.]"]]