Android剪贴板详解
前段时间忙着学东西做东西,这几天小马有时间就拿来学习下官方的文档,里面好东西太多了,今天看到了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. &n
补充:移动开发 , Android ,