Android剪贴板用法详解
程序员文章站
2022-08-17 19:28:52
本文实例详述了android剪贴板的用法,分享给大家供大家参考。具体方法分析如下:
这里首先需要注意的一点,就是在使用android剪贴板的时候大家只记住一点就行了,不管...
本文实例详述了android剪贴板的用法,分享给大家供大家参考。具体方法分析如下:
这里首先需要注意的一点,就是在使用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内容 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();
}
}
}
}
}
}
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内容 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,代码更简单:
复制代码 代码如下:
package com.xiaoma.clipboard.demo;
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值
* @author mzh
*/
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));
}
}
}
}
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值
* @author mzh
*/
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));
}
}
}
}
全局配置文件里面没什么,如下:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns: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>
<manifest xmlns: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>
希望本文所述对大家的android程序设计有所帮助。