android implements the text copy to clipboard function of ClipboardManager

  • 2020-05-27 07:04:53
  • OfStack

Note: when guiding packages

Before API 11: android.text.ClipboardManager
After API 11: android.content.ClipboardManager


/** 
*  Achieve text copy function  
* add by wangqianzhou 
* @param content 
*/  
public static void copy(String content, Context context)  
{  
//  Get the clipboard manager   
ClipboardManager cmb = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);  
cmb.setText(content.trim());  
}  
/** 
*  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 an ClipboardManager object, cmb, and associate it with the system clipboard. You can then copy content of type String to the clipboard using the setText(CharSequence text) function. In addition, the ClipboardManager class also provides the abstract CharSequence getText() function and abstract boolean hasText(), which can get the string content in the clipboard and query whether the clipboard is currently holding content. There are two versions of the ClipboardManager class, which USES a string-only clipboard manager that has been supported since API Level 1, and a newer version of the ClipboardManager class since Android 3.0 (API Level 11). Please refer to the official documentation for details.

Previous versions of android2.1 used the following approach


IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard")); 
clip.getClipboardText().toString();// Get the copied content  
clip.setClipboardText(text);// Set up the Clipboard  The content of the 


Related articles: