当前位置: 安卓之星 -> 安卓开发 -> Android剪贴板DEMO详解

Android剪贴板DEMO详解

作者:网络 发表于: 2016-12-22 点击: 376 次

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: 剪贴板学习
*/
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内容
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();
}
}
}
}
}
}
下面是用来接收Intent传递的值,临时的一个Activity,代码更简单:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.TextureView;
import android.widget.TextView;
/**
* @Title: ReceiverClip.java
* @Package com.xiaoma.clipboard.demo
* @Description:临时用来接收下从Clip传过来的Intent值
*/
public class ReceiverClip extends Activity {
private TextView tv1 ;
private TextView tv2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
init();
}
private void init(){
tv1 = (TextView)findViewById(R.id.xiaoma);
tv2 = (TextView)findViewById(R.id.yatou);
Intent intent = getIntent();
Bundle b =intent.getBundleExtra(“XiaoMaGuo”);
if(b != null){
int xiaoma = b.getInt(“xiaoma”);
int yatou = b.getInt(“yatou”);
if(!””.equals(String.valueOf(xiaoma)) && !””.equals(String.valueOf(yatou))){
tv1.setText(String.valueOf(xiaoma));
tv2.setText(String.valueOf(yatou));
}
}
}
}[/mw_shl_code]
全局配置文件里面没什么,如下:
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”     package=”com.xiaoma.clipboard.demo”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk android:minSdkVersion=”14″ />
<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” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.ReceiverClip”></activity>
</application>
</manifest>

相关文章

相关文章

赶快留言冒泡

  • 评论 (0)
  • 引用通告 (0)
目前还没有任何评论.
目前还没有任何Trackbacks和Pingbacks.
吐个泡浮上去.