这里首先需要注意的一点,就是在使用Android剪贴板的时候大家只记住一点就行了,不管是安卓设备还是PC机,复制粘贴在同一时间里只能用于一个对象上,整通俗点就是:PC机上,不可能同时从C盘复制,又从D盘复制就行了,具体的看代码,很简单,直接上代码:
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: 剪贴板学习
* @author MZH
*/
public class ClipBoardDemoActivity extends Activity implements OnClickListener{
private Button put = null;
private Button get = null;
private ClipboardManager clipboard = null;
private static 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();
}
/**
* 初始化方法实现
*/
private void init(){
put = (Button)findViewById(R.id.button1);
put.setOnClickListener(this);
get = (Button)findViewById(R.id.button2);
get.setOnClickListener(this);
}
/**
* 监听实现
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
put();
break;
case R.id.button2:
get();
break;
default:
break;
}
}
/**
* 往Clip中放入数据
*/
private void put(){
/**
* 往ClipboardManager中可放的数据类型有三种:
* 因为大家都知道,就算是电脑,Ctrl+c也不可能在同一时间里即
* 从C盘剪贴,又从D般剪贴,所以小马只写一种简单的信息进去,
* 另外两种写在注释中
//类型一:text
clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData textCd = ClipData.newPlainText("kkk", "WaHouHou!Clip....");
clipboard.setPrimaryClip(textCd);
*/
/**
*
//类型二:URI
Uri copyUri = Uri.parse(CONTACTS + COPY_PATH + "/" + "XiaoMa");
ClipData clipUri = ClipData.newUri(getContentResolver(),"URI",copyUri);
clipboard.setPrimaryClip(clipUri);
*
*/
//类型三:Intent
//试下在Intent剪贴时使用Bundle传值进去
clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
Intent appIntent = new Intent();
Bundle 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);
}
/**
* 从Clip中取数据
*/
private void get(){
clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
Item item = null;
//无数据时直接返回
if(!clipboard.hasPrimaryClip()){
Toast.makeText(getApplicationContext(), "剪贴板中无数据", Toast.LENGTH_SHORT).show();
return ;
}
//如果是文本信息
if (clipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_PLAIN)) {
ClipData cdText = clipboard.getPrimaryClip();
item = cdText.getItemAt(0);
//此处是TEXT文本信息
if(item.getText() == null){
Toast.makeText(getApplicationContext(), "剪贴板中无内容", Toast.LENGTH_SHORT).show();
return ;
}else{
Toast.makeText(getApplicationContext(), item.getText(), Toast.LENGTH_SHORT).show();
}
//如果是INTENT
} else if (clipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_INTENT)) {
//此处是INTENT
item = clipboard.getPrimaryClip().getItemAt(0);
Intent intent = item.getIntent();
startActivity(intent);
//........
//如果是URI
} else if (clipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_URILIST)) {
//此处是URI内容
www.VeVB.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, null, null);
if (pasteCursor != null) {
if (pasteCursor.moveToFirst()) {
//此处对数据进行操作就可以了,前提是有权限
}
}
pasteCursor.close();
}
}
}
}
}
}