首页 > 代码库 > Android 使用剪切板传递数据
Android 使用剪切板传递数据
转载自 Android剪贴板详解 http://www.2cto.com/kf/201203/123455.html
前段时间忙着学东西做东西,这几天小马有时间就拿来学习下官方的文档,里面好东西太多了,今天看到了Clip,反正不懂,小马带着无耻的好奇心,做了个DEMO,先讲下注意的一点,就是在使用Android剪贴板的时候大家只记住一点就行了,不管是安卓设备还是PC机,复制粘贴在同一时间里只能用于一个对象上,整通俗点就是:PC机上,不可能同时从C盘复制,又从D盘复制就行了,具体的看代码,很简单,直接上代码:
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: 剪贴板学习
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. * 初始化方法实现
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. * 监听实现
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. * 往Clip中放入数据
69. */
70. private void put(){
71.
72. /**
73. * 往ClipboardManager中可放的数据类型有三种:
74. * 因为大家都知道,就算是电脑,Ctrl+c也不可能在同一时间里即
75. * 从C盘剪贴,又从D般剪贴,所以小马只写一种简单的信息进去,
76. * 另外两种写在注释中
77.
78. //类型一:text
79. clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
80. ClipData textCd = ClipData.newPlainText("kkk", "WaHouHou!Clip....");
81. clipboard.setPrimaryClip(textCd);
82. */
83. /**
84. *
85. //类型二: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. //类型三:Intent
92. //试下在Intent剪贴时使用Bundle传值进去
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. * 从Clip中取数据
106. */
107. private void get(){
108. clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
109. Item item = null;
110.
111. //无数据时直接返回
112. if(!clipboard.hasPrimaryClip()){
113. Toast.makeText(getApplicationContext(), "剪贴板中无数据", Toast.LENGTH_SHORT).show();
114. return ;
115. }
116.
117. //如果是文本信息
118. if (clipboard.getPrimaryClipDescription().hasMimeType(
119. ClipDescription.MIMETYPE_TEXT_PLAIN)) {
120. ClipData cdText = clipboard.getPrimaryClip();
121. item = cdText.getItemAt(0);
122. //此处是TEXT文本信息
123. if(item.getText() == null){
124. Toast.makeText(getApplicationContext(), "剪贴板中无内容", Toast.LENGTH_SHORT).show();
125. return ;
126. }else{
127. Toast.makeText(getApplicationContext(), item.getText(), Toast.LENGTH_SHORT).show();
128. }
129.
130. //如果是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. //如果是URI
140. } else if (clipboard.getPrimaryClipDescription().hasMimeType(
141. ClipDescription.MIMETYPE_TEXT_URILIST)) {
142. //此处是URI内容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, null, null, null);
152. if (pasteCursor != null) {
153. if (pasteCursor.moveToFirst()) {
154. //此处对数据进行操作就可以了,前提是有权限
155. }
156. }
157. pasteCursor.close();
158. }
159. }
160. }
161. }
162. }
163. }
下面是用来接收Intent传递的值,临时的一个Activity,代码更简单:
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:临时用来接收下从Clip传过来的Intent值
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. }
全局配置文件里面没什么,如下:
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="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>
如果因为没效果图看不爽的话,朋友们可以直接下载小马写的DEMO,代码很简单,大牛跳过,跟小马一样的菜菜可以学习交流下,吼吼...老规矩,如有小马讲不清楚的地方,还请朋友直接批评指点,有错必改,共同提高进步,谢谢
Android 使用剪切板传递数据