Android uses the Clipboard to pass data

Source: Internet
Author: User

Reprinted from Android Clipboard detailed http://www.2cto.com/kf/201203/123455.html

Some time ago busy learning things to do things, these days pony have time to take to learn the official documents, inside good things too much, today saw clip, anyway do not understand, pony with shameless curiosity, did a demo, first to note the point of attention, is in the use of Android clipboard when everyone just remember a little, whether it is Android device or PC, copy and paste at the same time can only be used for an object, the whole popular point is: PC, it is impossible to copy from the C disk at the same time, from the D disk copy on the line, the specific look at the code, very simple, Directly on the code:

1. Package Com.xiaoma.clipboard.demo;
2.
3. Import android.app.Activity;
4. Import Android.content.ClipData;
5. Import Android.content.ClipData.Item;
6. Import Android.content.ClipDescription;
7. Import Android.content.ClipboardManager;
8. Import Android.content.ContentResolver;
9. Import Android.content.Intent;
. import Android.database.Cursor;
One. 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;
17.
18./**
* @Title: Clipboarddemoactivity.java
* @Package Com.xiaoma.clipboard.demo
* @Description: Clipboard Learning
* @author Mzh
23. */
public class Clipboarddemoactivity extends Activity implements onclicklistener{
25.
A. Private Button put = null;
Private Button get = null;
Clipboardmanager private clipboard = null;
private static final String CONTACTS = "Content://com.example.contacts";
A. Private String Copy_path = "/copy";
public static final String mime_type_contact = "Vnd.android.cursor.item/vnd.xiaoma.contact";
@Override.
OnCreate public void (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
(). Init ();
37.}
38.
39./**
40. * Initialization Method implementation
41. */
private void init () {
put = (Button) Findviewbyid (R.id.button1);
Put.setonclicklistener (this);
45.
A. Get = (Button) Findviewbyid (R.id.button2);
Get.setonclicklistener (this);
48.}
49.
50./**
51. * Monitor Implementation
52. */
@Override.
A. public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.button1:
. put ();
break;
Case R.id.button2:
A. get ();
break;
. Default:
break;
64.}
65.}
66.
67./**
68. * Put data into clip
69. */
A. private void put () {
71.
72./**
73. * There are three types of data that can be put into Clipboardmanager:
74. * Because everyone knows that even a computer, CTRL + C can not be at the same time
75. * Clip from C drive, and cut from d like, so pony only write a simple message to go in,
76. * Two other types are written in the comments
77.
78.//Type one: text
Clipboard = (Clipboardmanager) getsystemservice (Clipboard_service);
Clipdata TEXTCD = Clipdata.newplaintext ("KKK", "wahouhou! Clip .... ");
Bayi. Clipboard.setprimaryclip (TEXTCD);
82. */
83./**
84. *
85.//Type two: URI
Copyuri Uri = uri.parse (CONTACTS + Copy_path + "/" + "Xiaoma");
Clipdata Clipuri = Clipdata.newuri (Getcontentresolver (), "URI", Copyuri);
Clipboard.setprimaryclip (Clipuri);
89. *
90. */
91.//Type three: Intent
92.//try to use the bundle to pass the value in intent clip
Clipboard = (Clipboardmanager) getsystemservice (Clipboard_service);
94. Intent appintent = new Intent ();
Bundle bundle = new bundle ();
Bundle.putint ("Xiaoma", 3344258);
Bundle.putint ("Yatou", 3344179);
98. Appintent.putextra ("Xiaomaguo", bundle);
Appintent.setclass (Clipboarddemoactivity.this, Receiverclip.class);
Clipdata clipintent = clipdata.newintent ("Intent", appintent);
101. Clipboard.setprimaryclip (Clipintent);
102.}
103.
104./**
105. * Take data from clip
106. */
107. private void Get () {
108. Clipboard = (Clipboardmanager) getsystemservice (Clipboard_service);
109. Item item = NULL;
110.
111.//Direct return without data
The. if (!clipboard.hasprimaryclip ()) {
113. Toast.maketext (Getapplicationcontext (), "No data in clipboard", Toast.length_short). Show ();
A. return;
115.}
116.
117.//If it is a text message
118. if (Clipboard.getprimaryclipdescription (). Hasmimetype (
119. Clipdescription.mimetype_text_plain)) {
Clipdata Cdtext = Clipboard.getprimaryclip ();
121. Item = Cdtext.getitemat (0);
122.//Here is text message
123. if (item.gettext () = = null) {
124. Toast.maketext (Getapplicationcontext (), "No content in clipboard", Toast.length_short). Show ();
A. return;
126.}else{
127. Toast.maketext (Getapplicationcontext (), Item.gettext (), Toast.length_short). Show ();
128.}
129.
130.//If it is intent
131.} else if (Clipboard.getprimaryclipdescription (). Hasmimetype (
Clipdescription.mimetype_text_intent)) {
133.//Here is intent
134. Item = Clipboard.getprimaryclip (). Getitemat (0);
135. Intent Intent = Item.getintent ();
136. StartActivity (Intent);
137.//.....
138.
139.//If it is a URI
Clipboard.getprimaryclipdescription.} else if (Hasmimetype ().
141. Clipdescription.mimetype_text_urilist)) {
142.//Here is the URI content www.2cto.com
143. Contentresolver cr = Getcontentresolver ();
144. Clipdata Cduri = Clipboard.getprimaryclip ();
145. Item = Cduri.getitemat (0);
146. Uri uri = Item.geturi ();
147. if (uri! = null) {
148. String MimeType = Cr.gettype (URI);
149. if (mimeType! = null) {
if (Mimetype.equals (mime_type_contact)) {
151. Cursor pastecursor = cr.query (URI, NULL, NULL, NULL, NULL);
if (pastecursor! = null) {
153. if (Pastecursor.movetofirst ()) {
154.//Here to operate the data, if you have permission
155.}
156.}
157. Pastecursor.close ();
158.}
159.}
160.}
161.}
162.}
163.}
Here is the value to receive the intent pass, a temporary activity, the code is simpler:

1. Package Com.xiaoma.clipboard.demo;
2.
3. Import android.app.Activity;
4. Import Android.content.Intent;
5. Import Android.os.Bundle;
6. Import Android.view.TextureView;
7. Import Android.widget.TextView;
8.
9./**
* @Title: Receiverclip.java
* @Package Com.xiaoma.clipboard.demo
* @Description: Temporarily used to receive intent values from clip
* @author Mzh
14. */
public class Receiverclip extends Activity {
16.
. private TextView TV1;
Private TextView TV2;
19.
@Override
protected void OnCreate (Bundle savedinstancestate) {
//TODO auto-generated method stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.main2);
Init ();
26.}
27.
. private void Init () {
29.
TV1 = (TextView) Findviewbyid (R.id.xiaoma);
TV2 = (TextView) Findviewbyid (R.id.yatou);
32.
Intent Intent = Getintent ();
Bundle b =intent.getbundleextra ("Xiaomaguo");
if (b! = null) {
Xiaoma int = b.getint ("Xiaoma");
PNs. int Yatou = B.getint ("Yatou");
. if (! "". Equals (String.valueof (xiaoma)) &&! "". Equals (String.valueof (Yatou))) {
Tv1.settext (string.valueof (Xiaoma));
Tv2.settext (string.valueof (Yatou));
41.}
42.}
43.}
44.}
There is nothing in the global configuration file, as follows:

1. <?xml version= "1.0" encoding= "Utf-8"?>
2. <manifest xmlns:android= ""
3. package= "Com.xiaoma.clipboard.demo"
4. android:versioncode= "1"
5. Android:versionname= "1.0" >
6.
7. <uses-sdk android:minsdkversion= "/>"
8.
9. <application
android:icon= "@drawable/guoguo"
Android:label= "@string/app_name" >
<activity.
Android:name= ". Clipboarddemoactivity "
Android:label= "@string/app_name" >
<intent-filter>.
<action android:name= "Android.intent.action.MAIN"/>
17.
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". Receiverclip "></activity>
</application>
23.
</manifest>
If you do not see the bad, friends can directly download the pony to write the demo, the code is very simple, Daniel skipped, and pony-like vegetables can learn to exchange, roar ... The usual, such as the small horse is not clear where, but also ask a friend direct criticism, mistakes will change, together improve progress, thank you

Android uses the Clipboard to pass data

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.