Android clipboard details

Source: Internet
Author: User

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:
 
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;
10. import android. database. Cursor;
11. import android.net. Uri;
12. import android. OS. Bundle;
13. import android. view. View;
14. import android. view. View. OnClickListener;
15. import android. widget. Button;
16. import android. widget. Toast;
17.
18 ./**
19. * @ Title: ClipBoardDemoActivity. java
20. * @ Package com. xiaoma. clipboard. demo
21. * @ Description: clipboard Learning
22. * @ author MZH
23 .*/
24. public class ClipBoardDemoActivity extends Activity implements OnClickListener {
25.
26. private Button put = null;
27. private Button get = null;
28. private ClipboardManager clipboard = null;
29. private static final String CONTACTS = "content: // com. example. contacts ";
30. private String COPY_PATH = "/copy ";
31. public static final String MIME_TYPE_CONTACT = "vnd. android. cursor. item/vnd. xiaoma. contact ";
32. @ Override
33. public void onCreate (Bundle savedInstanceState ){
34. super. onCreate (savedInstanceState );
35. setContentView (R. layout. main );
36. init ();
37 .}
38.
39 ./**
40. * initialization method implementation
41 .*/
42. private void init (){
43. put = (Button) findViewById (R. id. button1 );
44. put. setOnClickListener (this );
45.
46. get = (Button) findViewById (R. id. button2 );
47. get. setOnClickListener (this );
48 .}
49.
50 ./**
51. * listener implementation
52 .*/
53. @ Override
54. public void onClick (View v ){
55. switch (v. getId ()){
56. case R. id. button1:
57. put ();
58. break;
59. case R. id. button2:
60. get ();
61. break;
62. default:
63. break;
64 .}
65 .}
66.
67 ./**
68. * put data into Clip
69 .*/
70. private void put (){
71.
72 ./**
73. * There are three data types available to ClipboardManager:
74. * as we all know, even a computer, Ctrl + c cannot be
75. * cut the paste from drive C and cut it from drive D, So pony only writes a simple information,
76. * The other two are written in comments.
77.
78. // type 1: text
79. clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE );
80. ClipData textCd = ClipData. newPlainText ("kkk", "WaHouHou! Clip ....");
81. clipboard. setPrimaryClip (textCd );
82 .*/
83 ./**
84 .*
85. // type 2: URI
86. Uri copyUri = Uri. parse (CONTACTS + COPY_PATH + "/" + "XiaoMa ");
87. ClipData clipUri = ClipData. newUri (getContentResolver (), "URI", copyUri );
88. clipboard. setPrimaryClip (clipUri );
89 .*
90 .*/
91. // Type 3: Intent
92. // try to use Bundle to pass the value in the Intent clipboard.
93. clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE );
94. Intent appIntent = new Intent ();
95. Bundle bundle = new Bundle ();
96. bundle. putInt ("xiaoma", 3344258 );
97. bundle. putInt ("yatou", 3344179 );
98. appIntent. putExtra ("XiaoMaGuo", bundle );
99. appIntent. setClass (ClipBoardDemoActivity. this, ReceiverClip. class );
100. ClipData clipIntent = ClipData. newIntent ("Intent", appIntent );
101. clipboard. setPrimaryClip (clipIntent );
102 .}
103.
104 ./**
105. * fetch data from Clip
106 .*/
107. private void get (){
108. clipboard = (ClipboardManager) getSystemService (CLIPBOARD_SERVICE );
109. Item item = null;
110.
111. // return directly if no data exists
112. if (! Clipboard. hasPrimaryClip ()){
113. Toast. makeText (getApplicationContext (), "no data in Clipboard", Toast. LENGTH_SHORT). show ();
114. return;
115 .}
116.
117. // For text information
118. if (clipboard. getPrimaryClipDescription (). hasMimeType (
119. ClipDescription. MIMETYPE_TEXT_PLAIN )){
120. ClipData cdText = clipboard. getPrimaryClip ();
121. item = cdText. getItemAt (0 );
122. // TEXT information
123. if (item. getText () = null ){
124. Toast. makeText (getApplicationContext (), "NO content in Clipboard", Toast. LENGTH_SHORT). show ();
125. return;
126.} else {
127. Toast. makeText (getApplicationContext (), item. getText (), Toast. LENGTH_SHORT). show ();
128 .}
129.
130. // For INTENT
131.} else if (clipboard. getPrimaryClipDescription (). hasMimeType (
132. ClipDescription. MIMETYPE_TEXT_INTENT )){
133. // INTENT
134. item = clipboard. getPrimaryClip (). getItemAt (0 );
135. Intent intent = item. getIntent ();
136. startActivity (intent );
137 .//........
138.
139. // if it is a URI
140.} else if (clipboard. getPrimaryClipDescription (). 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 ){
150. if (mimeType. equals (MIME_TYPE_CONTACT )){
151. Cursor pasteCursor = cr. query (uri, null );
152. if (pasteCursor! = Null ){
153. if (pasteCursor. moveToFirst ()){
154. // you can operate the data only if you have the permission.
155 .}
156 .}
157. pasteCursor. close ();
158 .}
159 .}
160 .}
161 .}
162 .}
163 .}
The following is a temporary Activity that is used to receive values passed by Intent. 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 ./**
10. * @ Title: ReceiverClip. java
11. * @ Package com. xiaoma. clipboard. demo
12. * @ Description: temporarily used to receive the Intent value passed from Clip
13. * @ author MZH
14 .*/
15. public class ReceiverClip extends Activity {
16.
17. private TextView tv1;
18. private TextView tv2;
19.
20. @ Override
21. protected void onCreate (Bundle savedInstanceState ){
22. // TODO Auto-generated method stub
23. super. onCreate (savedInstanceState );
24. setContentView (R. layout. main2 );
25. init ();
26 .}
27.
28. private void init (){
29.
30. tv1 = (TextView) findViewById (R. id. xiaoma );
31. tv2 = (TextView) findViewById (R. id. yatou );
32.
33. Intent intent = getIntent ();
34. Bundle B = intent. getBundleExtra ("XiaoMaGuo ");
35. if (B! = Null ){
36. int xiaoma = B. getInt ("xiaoma ");
37. int yatou = B. getInt ("yatou ");
38. if (! "". Equals (String. valueOf (xiaoma ))&&! "". Equals (String. valueOf (yatou ))){
39. tv1.setText (String. valueOf (xiaoma ));
40. tv2.setText (String. valueOf (yatou ));
41 .}
42 .}
43 .}
44 .}
There is nothing in the global configuration file, as shown below:
 
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" type = "codeph" text = "/codeph">
6.
7. <uses-sdk android: minSdkVersion = "14"/>
8.
9. <application
10. android: icon = "@ drawable/guoguo"
11. android: label = "@ string/app_name">
12. <activity
13. android: name = ". ClipBoardDemoActivity"
14. android: label = "@ string/app_name">
15. <intent-filter>
16. <action android: name = "android. intent. action. MAIN"/>
17.
18. <category android: name = "android. intent. category. LAUNCHER"/>
19. </intent-filter>
20. </activity>
21. <activity android: name = ". ReceiverClip"> </activity>
22. </application>
23.
24. </manifest>
If you don't understand it, you can download the DEMO written by pony directly. The code is very simple. You can skip the DEMO, and enjoy the same dishes as pony... old Rules: If the pony cannot tell me clearly, ask your friends to criticize and give advice directly. If there are any mistakes, you must correct them and make improvements together. Thank you.

 

From cool _ inexplicable and simple

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.