Android clipboard operation method is used in different versions of API, androidapi
Before SDK11, use android. text. ClipboardManager
Java code
- Import android. text. ClipboardManager;
- ......
- ClipboardManager clipboardManager = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE );
- ClipboardManager. setText ("content ");
- If (clipboardManager. hasText ()){
- ClipboardManager. getText ();
- }
Android. text. ClipboardManager has been deprecated since SDK11 and replaced by its subclass android. content. ClipboardManager. The setText/getText/hasText method has also been deprecated and is replaced by setPrimaryClip/getPrimaryClip
Java code
- Import android. content. ClipboardManager;
- ......
- ClipboardManager clipboardManager = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE );
- ClipboardManager. setPrimaryClip (ClipData. newPlainText (null, "content "));
- If (clipboardManager. hasPrimaryClip ()){
- ClipboardManager. getPrimaryClip (). getItemAt (0). getText ();
- }
- Public class ClipboardUtils {
@ SuppressWarnings ("deprecation ")
Public static void copyToClipboard (Context context, String text ){
Android. text. ClipboardManager cbm = (android. text. ClipboardManager) context. getSystemService (Context. CLIPBOARD_SERVICE );
Cbm. setText (text );
}
@ SuppressWarnings ("deprecation ")
Public static CharSequence getClipboardText (Context context ){
Android. text. ClipboardManager cbm = (android. text. ClipboardManager) context. getSystemService (Context. CLIPBOARD_SERVICE );
Return cbm. getText ();
}
}