Android provides a powerful clipboard-based framework for copying and pasting. It supports both simple and complex data types, including text strings, complex data structures, text and binary stream data, and even application assets. Simple text data is stored directly in the clipboard, while complex data is stored as a reference that the pasting application resolves with a content provider. Copying and pasting works both within an application and between applications that implement the framework.
Since a part of the framework uses content providers, this topic assumes some familiarity with the Android Content Provider API, which is described in the topic Content Providers.
The Clipboard Framework
When you use the clipboard framework, you put data into a clip object, and then put the clip object on the system-wide clipboard. The clip object can take one of three forms:
- Text
- A text string. You put the string directly into the clip object, which you then put onto the clipboard. To paste the string, you get the clip object from the clipboard and copy the string to into your application’s storage.
- URI
- A
Uri
object representing any form of URI. This is primarily for copying complex data from a content provider. To copy data, you put aUri
object into a clip object and put the clip object onto the clipboard. To paste the data, you get the clip object, get theUri
object, resolve it to a data source such as a content provider, and copy the data from the source into your application’s storage. - Intent
- An
Intent
. This supports copying application shortcuts. To copy data, you create an Intent, put it into a clip object, and put the clip object onto the clipboard. To paste the data, you get the clip object and then copy the Intent object into your application’s memory area.
The clipboard holds only one clip object at a time. When an application puts a clip object on the clipboard, the previous clip object disappears.
If you want to allow users to paste data into your application, you don’t have to handle all types of data. You can examine the data on the clipboard before you give users the option to paste it. Besides having a certain data form, the clip object also contains metadata that tells you what MIME type or types are available. This metadata helps you decide if your application can do something useful with the clipboard data. For example, if you have an application that primarily handles text, you may want to ignore clip objects that contain a URI or Intent.
You may also want to allow users to paste text regardless of the form of data on the clipboard. To do this, you can force the clipboard data into a text representation, and then paste this text. This is described in the section Coercing the clipboard to text.
Copying to the Clipboard
As described previously, to copy data to the clipboard you get a handle to the global ClipboardManager
object, create a ClipData
object, add a ClipDescription
and one or more ClipData.Item
objects to it, and add the finished ClipData
object to the ClipboardManager
object. This is described in detail in the following procedure:
- If you are copying data using a content URI, set up a content provider.The Note Pad sample application is an example of using a content provider for copying and pasting. The NotePadProvider class implements the content provider. The NotePad class defines a contract between the provider and other applications, including the supported MIME types.
- Get the system clipboard Copy the data to a new
ClipData
object: -
For a URI
This snippet constructs a URI by encoding a record ID onto the content URI for the provider. This technique is covered in more detail in the section Encoding an identifier on the URI:
Comments (0)