Android also has a clipboard (ClipboardManager), which can copy some useful text to the clipboard, so that the user can use it where it can be pasted. The following is how to use it
Note: when guiding the package
Before API 11: android.text.ClipboardManager
After API 11: android.content.ClipboardManager
/**
* Implement text copy function
* add by wangqianzhou
* @param content
*/
public static void copy(String content, Context context)
{
// Get
ClipboardManager cmb = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE );
cmb.setText(content.trim());
}
/**
* Implement the paste function
* add by wangqianzhou
* @param context
* @return
*/
public static String paste(Context context)
{
// Get the clipboard manager
ClipboardManager cmb = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
return cmb.getText().toString().trim();
}
First create a ClipboardManager object cmb and associate it with the system clipboard. After that, the content of String type can be copied to the clipboard through the setText(CharSequence text) function. In addition, the ClipboardManager class also provides the abstract CharSequence getText() function and the abstract boolean hasText() function, which can obtain the string content in the clipboard and query whether the clipboard currently has content. There are two versions of the ClipboardManager class. The clipboard manager that can only save strings, which has been supported since API Level 1, is used here. Since Android 3.0 (API Level 11), the new version of the ClipboardManager class supports more functions. . See the official documentation for details.
Versions before android2.1 use the following methods
IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
clip.getClipboardText().toString();//Get the copied content
clip.setClipboardText(text);//Set the content of Clipboard