Android clipboard setprimaryclip

Source: Internet
Author: User

(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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.