Stay organized with collections
Save and categorize content based on your preferences.
OnReceiveContentListener
public
interface
OnReceiveContentListener
android.view.OnReceiveContentListener
|
Listener for apps to implement handling for insertion of content. Content may be both text and
non-text (plain/styled text, HTML, images, videos, audio files, etc).
This listener can be attached to different types of UI components using
View.setOnReceiveContentListener
.
Here is a sample implementation that handles content URIs and delegates the processing for
text and everything else to the platform:
// (1) Define the listener
public class MyReceiver implements OnReceiveContentListener {
public static final String[] MIME_TYPES = new String[] {"image/*", "video/*"};
@Override
public ContentInfo onReceiveContent(View view, ContentInfo payload) {
Pair<ContentInfo, ContentInfo> split =
ContentInfoCompat.partition(payload, item -> item.getUri() != null);
ContentInfo uriContent = split.first;
ContentInfo remaining = split.second;
if (uriContent != null) {
ClipData clip = uriContent.getClip();
for (int i = 0; i < clip.getItemCount(); i++) {
Uri uri = clip.getItemAt(i).getUri();
// ... app-specific logic to handle the URI ...
}
}
// Return anything that we didn't handle ourselves. This preserves the default platform
// behavior for text and anything else for which we are not implementing custom handling.
return remaining;
}
}
// (2) Register the listener
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
EditText myInput = findViewById(R.id.my_input);
myInput.setOnReceiveContentListener(MyReceiver.MIME_TYPES, new MyReceiver());
}
Summary
Public methods
onReceiveContent
public abstract ContentInfo onReceiveContent (View view,
ContentInfo payload)
Receive the given content.
Implementations should handle any content items of interest and return all unhandled
items to preserve the default platform behavior for content that does not have app-specific
handling. For example, an implementation may provide handling for content URIs (to provide
support for inserting images, etc) and delegate the processing of text to the platform to
preserve the common behavior for inserting text. See the class javadoc for a sample
implementation.
Handling different content
- Text. If the
source
is
autofill
, the view's content should be fully
replaced by the passed-in text. For sources other than autofill, the passed-in text
should overwrite the current selection or be inserted at the current cursor position
if there is no selection.
- Non-text content (e.g. images). The content may be inserted inline if the widget
supports this, or it may be added as an attachment (could potentially be shown in a
completely separate view).
URI permissions
Read permissions
are
granted automatically by the platform for any
content URIs
in the payload passed
to this listener. Permissions are transient and will be released automatically by the
platform.
Processing of content should normally be done in a service or activity.
For long-running processing, using androidx.work.WorkManager
is recommended.
When implementing this, permissions should be extended to the target service or activity
by passing the content using Intent.setClipData
and setting
the flag
FLAG_GRANT_READ_URI_PERMISSION
.
Alternatively, if using a background thread within the current context to process the
content, a reference to the payload
object should be maintained to ensure that
permissions are not revoked prematurely.
Parameters |
view |
View : The view where the content insertion was requested.
This value cannot be null . |
payload |
ContentInfo : The content to insert and related metadata. The payload may contain multiple
items and their MIME types may be different (e.g. an image item and a text
item). The payload may also contain items whose MIME type is not in the list
of MIME types specified when
setting the listener. For
those items, the listener may reject the content (defer to the default
platform behavior) or execute some other fallback logic (e.g. show an
appropriate message to the user).
This value cannot be null . |
Returns |
ContentInfo |
The portion of the passed-in content whose processing should be delegated to
the platform. Return null if all content was handled in some way. Actual insertion of
the content may be processed asynchronously in the background and may or may not
succeed even if this method returns null. For example, an app may end up not inserting
an item if it exceeds the app's size limit for that type of content. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# OnReceiveContentListener\n\nAdded in [API level 31](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\nOnReceiveContentListener\n========================\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/view/OnReceiveContentListener \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\ninterface\nOnReceiveContentListener\n`\n\n\n`\n\n\n`\n\n|---------------------------------------|\n| android.view.OnReceiveContentListener |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nListener for apps to implement handling for insertion of content. Content may be both text and\nnon-text (plain/styled text, HTML, images, videos, audio files, etc).\n\nThis listener can be attached to different types of UI components using\n[View.setOnReceiveContentListener](/reference/android/view/View#setOnReceiveContentListener(java.lang.String[],%20android.view.OnReceiveContentListener)).\n\nHere is a sample implementation that handles content URIs and delegates the processing for\ntext and everything else to the platform: \n\n```gdscript\n // (1) Define the listener\n public class MyReceiver implements OnReceiveContentListener {\n public static final String[] MIME_TYPES = new String[] {\"image/*\", \"video/*\"};\n\n @Override\n public ContentInfo onReceiveContent(View view, ContentInfo payload) {\n Pair\u003cContentInfo, ContentInfo\u003e split =\n ContentInfoCompat.partition(payload, item -\u003e item.getUri() != null);\n ContentInfo uriContent = split.first;\n ContentInfo remaining = split.second;\n if (uriContent != null) {\n ClipData clip = uriContent.getClip();\n for (int i = 0; i \u003c clip.getItemCount(); i++) {\n Uri uri = clip.getItemAt(i).getUri();\n // ... app-specific logic to handle the URI ...\n }\n }\n // Return anything that we didn't handle ourselves. This preserves the default platform\n // behavior for text and anything else for which we are not implementing custom handling.\n return remaining;\n }\n }\n\n // (2) Register the listener\n public class MyActivity extends Activity {\n @Override\n public void onCreate(Bundle savedInstanceState) {\n // ...\n\n EditText myInput = findViewById(R.id.my_input);\n myInput.setOnReceiveContentListener(MyReceiver.MIME_TYPES, new MyReceiver());\n }\n \n```\n\n\u003cbr /\u003e\n\nSummary\n-------\n\n| ### Public methods ||\n|----------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract `[ContentInfo](/reference/android/view/ContentInfo) | ` `[onReceiveContent](/reference/android/view/OnReceiveContentListener#onReceiveContent(android.view.View,%20android.view.ContentInfo))`(`[View](/reference/android/view/View)` view, `[ContentInfo](/reference/android/view/ContentInfo)` payload) ` Receive the given content. |\n\nPublic methods\n--------------\n\n### onReceiveContent\n\nAdded in [API level 31](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract ContentInfo onReceiveContent (View view, \n ContentInfo payload)\n```\n\nReceive the given content.\n\nImplementations should handle any content items of interest and return all unhandled\nitems to preserve the default platform behavior for content that does not have app-specific\nhandling. For example, an implementation may provide handling for content URIs (to provide\nsupport for inserting images, etc) and delegate the processing of text to the platform to\npreserve the common behavior for inserting text. See the class javadoc for a sample\nimplementation.\n\n### Handling different content\n\n- Text. If the [source](/reference/android/view/ContentInfo#getSource()) is [autofill](/reference/android/view/ContentInfo#SOURCE_AUTOFILL), the view's content should be fully replaced by the passed-in text. For sources other than autofill, the passed-in text should overwrite the current selection or be inserted at the current cursor position if there is no selection.\n- Non-text content (e.g. images). The content may be inserted inline if the widget supports this, or it may be added as an attachment (could potentially be shown in a completely separate view).\n\n### URI permissions\n\n[Read permissions](/reference/android/content/Intent#FLAG_GRANT_READ_URI_PERMISSION) are\ngranted automatically by the platform for any\n[content URIs](/reference/android/content/ContentResolver#SCHEME_CONTENT) in the payload passed\nto this listener. Permissions are transient and will be released automatically by the\nplatform.\n\nProcessing of content should normally be done in a service or activity.\nFor long-running processing, using `androidx.work.WorkManager` is recommended.\nWhen implementing this, permissions should be extended to the target service or activity\nby passing the content using [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, if using a background thread within the current context to process the\ncontent, a reference to the `payload` object should be maintained to ensure that\npermissions are not revoked prematurely.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `view` | `View`: The view where the content insertion was requested. This value cannot be `null`. \u003cbr /\u003e |\n| `payload` | `ContentInfo`: The content to insert and related metadata. The payload may contain multiple items and their MIME types may be different (e.g. an image item and a text item). The payload may also contain items whose MIME type is not in the list of MIME types specified when [setting](/reference/android/view/View#setOnReceiveContentListener(java.lang.String[],%20android.view.OnReceiveContentListener)) the listener. For those items, the listener may reject the content (defer to the default platform behavior) or execute some other fallback logic (e.g. show an appropriate message to the user). This value cannot be `null`. \u003cbr /\u003e |\n\n| Returns ||\n|----------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [ContentInfo](/reference/android/view/ContentInfo) | The portion of the passed-in content whose processing should be delegated to the platform. Return null if all content was handled in some way. Actual insertion of the content may be processed asynchronously in the background and may or may not succeed even if this method returns null. For example, an app may end up not inserting an item if it exceeds the app's size limit for that type of content. \u003cbr /\u003e |"]]