(I) Pay attention to which version of the SDK you are using. It is different from 11.
> = 11
Android. Content
Public class
Clipboardmanager
Extends clipboardmanager (Abstruct content. Text)
(Ii) first look at your androidmanifest. xml
<Uses-SDK
Android: minsdkversion ="11"
Android: targetsdkversion = "16"/>
If the value is greater than or equal to 11 and less than 11, there is a large area.
A: greater than or equal to 11
1. Write to clipboard:
[Write text]
Clipboardmanager = (clipboardmanager) getsystemservice (clipboard_service );
Clipdata = clipdata. newplaintext ("newplaintextlabel ",
"Zhangyongbin ");
Clipboardmanager. setprimaryclip (clipdata );
[Write object]
Student = new student ("zhangyongbin", 23 );
Bytearrayoutputstream = new bytearrayoutputstream ();
Try {
Objectoutputstream = new objectoutputstream (bytearrayoutputstream );
Objectoutputstream. writeobject (student );
String base64string = base64.encodetostring (bytearrayoutputstream. tobytearray (), base64.default );
Item = new item (base64string );
Clipdata. additem (item );
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Clipboardmanager. setprimaryclip (clipdata );
2. Read the clipboard:
[Read text]
Clipboardmanager = (clipboardmanager) getsystemservice (clipboard_service );
String message = (string) clipboardmanager. getprimaryclip ()
. Getdescription (). getlabel ()
+ ""
+ (String) clipboardmanager. getprimaryclip (). getitemat (0)
. Gettext ();
[Read object]
Clipboardmanager = (clipboardmanager) getsystemservice (clipboard_service );
String message = (string) clipboardmanager. getprimaryclip ()
. Getdescription (). getlabel ()
+ ""
+ (String) clipboardmanager. getprimaryclip (). getitemat (0)
. Gettext ();
String message2 = (string) clipboardmanager. getprimaryclip ()
. Getitemat (1). gettext ();
Byte [] bytet = base64.decode (message2, base64.default );
Try {
Objectinputstream = new objectinputstream (
New bytearrayinputstream (bytet ));
Student = (student) objectinputstream. readobject ();
Message = student. tostring ();
} Catch (streamcompuptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (exception e ){
// Todo: handle exception
}
B: less than 11
Use settext and gettext.