Android clipboard details

Source: Internet
Author: User

I won't say much about what the clipboard is. Here we will only talk about the simplest application and paste general text.

Some time ago, I was busy learning things and doing things. In the past few days, my pony spent some time learning official documents. There were too many good things in it. Today I saw clip, but I don't understand it anyway, pony made a demo with shameless curiosity. Let's first note that when using the android clipboard, you only need to remember one thing, whether it's Android or PC, copy and paste can only be used on one object at a time. The general point is: on a PC, it is impossible to copy data from disk C at the same time, and copy data from disk D. For details, refer to the code, the code is very simple:


Package COM. xiaoma. clipboard. demo; import android. app. activity; import android. content. clipdata; import android. content. clipdata. item; import android. content. clipdescription; import android. content. clipboardmanager; import android. content. contentresolver; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. view. view; import Android. view. view. onclicklistener; import android. widget. button; import android. widget. toast;/*** @ title: clipboarddemoactivity. java * @ package COM. xiaoma. clipboard. demo * @ Description: clipboard learning * @ author mzh */public class clipboarddemoactivity extends activity implements onclicklistener {private button put = NULL; private button get = NULL; private clipboardmanager clipboard = NULL; private sta TIC final string contacts = "content: // COM. example. contacts "; private string copy_path ="/copy "; public static final string mime_type_contact =" Vnd. android. cursor. item/vnd. xiaoma. contact "; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init () ;}/ *** initialization method implementation */private void Init () {put = (button) findviewbyid (R. id. bu Tton1); Put. setonclicklistener (this); get = (button) findviewbyid (R. id. button2); get. setonclicklistener (this);}/*** listener implementation */@ override public void onclick (view v) {Switch (v. GETID () {case R. id. button1: Put (); break; case R. id. button2: Get (); break; default: break;}/*** put data into clip */private void put () {/*** there are three data types available to clipboardmanager: * as we all know, even if it is a computer, CTRL + C won't be able to cut the paste from drive C at the same time, and cut the stickers like D, so Pony writes only one simple information. * The other two types are written in the comment. // type 1: Text clipboard = (clipboardmanager) getsystemservice (clipboard_service); clipdata textcd = clipdata. newplaintext ("KKK", "wahouhou! Clip .... "); clipboard. setprimaryclip (textcd); * // *** // type 2: URI copyuri = Uri. parse (contacts + copy_path + "/" + "xiaoma"); clipdata clipuri = clipdata. newuri (getcontentresolver (), "Uri", copyuri); clipboard. setprimaryclip (clipuri); ** // Type 3: intent // use the bundle to pass the value to clipboard = (clipboardmanager) getsystemservice (clipboard_service) during intent clipboard ); intent appintent = new intent (); bundle = new Bundle (); bundle. putint ("xiaoma", 3344258); bundle. putint ("yatou", 3344179); appintent. putextra ("xiaomaguo", bundle); appintent. setclass (clipboarddemoactivity. this, receiverclip. class); clipdata clipintent = clipdata. newintent ("intent", appintent); clipboard. setprimaryclip (clipintent);}/*** get data from Clip */private void get () {clipboard = (clipboardmanager) getsystemservice (clipboard_service); item I TEM = NULL; // If (! Clipboard. hasprimaryclip () {toast. maketext (getapplicationcontext (), "no data in Clipboard", toast. length_short ). show (); return;} // if it is text information if (clipboard. getprimaryclipdescription (). hasmimetype (clipdescription. mimetype_text_plain) {clipdata cdtext = clipboard. getprimaryclip (); item = cdtext. getitemat (0); // here is the text information if (item. gettext () = NULL) {toast. maketext (getapplicationcontext (), "NO content in Clipboard", toast. leng Th_short ). show (); return;} else {toast. maketext (getapplicationcontext (), item. gettext (), toast. length_short ). show () ;}// if intent} else if (clipboard. getprimaryclipdescription (). hasmimetype (clipdescription. mimetype_text_intent )){. // intent item = clipboard. getprimaryclip (). getitemat (0); intent = item. getintent ();. startactivity (intent );//........ // If Uri} else if (clipboard. getpr Imaryclipdescription (). hasmimetype (clipdescription. mimetype_text_urilist) {// URI content www.2cto.com contentresolver Cr = getcontentresolver (); clipdata cduri = clipboard. getprimaryclip (); item = cduri. getitemat (0); Uri uri = item. geturi (); If (Uri! = NULL) {string mimetype = Cr. GetType (URI); If (mimetype! = NULL) {If (mimetype. Equals (mime_type_contact) {cursor pastecursor = Cr. Query (Uri, null, null); If (pastecursor! = NULL) {If (pastecursor. movetofirst () {// you can operate the data here, provided that you have the permission .}} pastecursor. close () ;}}}}below is used to receive the value passed by intent, a temporary activity, the code is simpler: Package COM. xiaoma. clipboard. demo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. textureview; import android. widget. textview;/*** @ title: receiverclip. java * @ package COM. xiaoma. clipboard. demo *@ Description: it is used to temporarily receive the intent value passed from Clip * @ author mzh */public class receiverclip extends activity {private textview TV1; private textview TV2; @ override protected void oncreate (bundle savedinstancestate) {// todo auto-generated method stub super. oncreate (savedinstancestate); setcontentview (R. layout. main2); Init ();} private void Init () {TV1 = (textview) findviewbyid (R. id. xiaoma); TV2 = (textview) Findviewbyid (R. Id. yatou); intent = getintent (); bundle B = intent. getbundleextra ("xiaomaguo"); If (B! = NULL) {int xiaoma = B. getint ("xiaoma"); int yatou = B. getint ("yatou"); If (! "". Equals (string. valueof (xiaoma ))&&! "". Equals (string. valueof (yatou) {tv1.settext (string. valueof (xiaoma); tv2.settext (string. valueof (yatou ));}}}}

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.