This example details the use of the Android Clipboard and shares it for your reference. The specific methods are analyzed as follows:
The first thing to note here is that when you use the Android clipboard, you can just remember a little bit, whether it is an Android device or a PC, copy and paste at the same time can only be used for an object, the whole popular point is: PC, it is not possible to copy from C disk at the same time, and from D disk copy on the line, The specific look code, very simple, directly on the code:
Copy Code code as follows:
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: Clipboard Learning
* @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 ();
}
/**
* Initialization Method implementation
*/
private void init () {
put = (Button) Findviewbyid (R.id.button1);
Put.setonclicklistener (this);
get = (Button) Findviewbyid (R.id.button2);
Get.setonclicklistener (this);
}
/**
* Monitor Implementation
*/
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.button1:
Put ();
Break
Case R.id.button2:
Get ();
Break
Default
Break
}
}
/**
* Put data into the clip
*/
private void put () {
/**
* There are three types of data available to Clipboardmanager:
* Because everyone knows that even a computer, CTRL + C can not be at the same time that
* from c disk clip, and from D-like clip, so pony only write a simple message to go in,
* The other two are written in the comments
Type one: text
Clipboard = (Clipboardmanager) getsystemservice (Clipboard_service);
Clipdata TEXTCD = Clipdata.newplaintext ("KKK", "wahouhou! Clip ... ");
Clipboard.setprimaryclip (TEXTCD);
*/
/**
*
Type two: URI
Uri Copyuri = uri.parse (CONTACTS + Copy_path + "/" + "Xiaoma");
Clipdata Clipuri = Clipdata.newuri (Getcontentresolver (), "URI", Copyuri);
Clipboard.setprimaryclip (Clipuri);
*
*/
Type III: Intent
Try using bundle in intent clip
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);
}
/**
* Fetching data from the clip
*/
private void Get () {
Clipboard = (Clipboardmanager) getsystemservice (Clipboard_service);
Item item = NULL;
Direct return when there is no data
if (!clipboard.hasprimaryclip ()) {
Toast.maketext (Getapplicationcontext (), "No data in clipboard", Toast.length_short). Show ();
return;
}
If this is a text message
if (Clipboard.getprimaryclipdescription (). Hasmimetype (
Clipdescription.mimetype_text_plain)) {
Clipdata Cdtext = Clipboard.getprimaryclip ();
item = Cdtext.getitemat (0);
Here is the text message
if (item.gettext () = null) {
Toast.maketext (Getapplicationcontext (), "No content in clipboard", Toast.length_short). Show ();
return;
}else{
Toast.maketext (Getapplicationcontext (), Item.gettext (), Toast.length_short). Show ();
}
If it's intent,
else if (clipboard.getprimaryclipdescription (). Hasmimetype (
Clipdescription.mimetype_text_intent)) {
Here is intent
item = Clipboard.getprimaryclip (). Getitemat (0);
Intent Intent = Item.getintent ();
StartActivity (Intent);
//........
If it is a URI
else if (clipboard.getprimaryclipdescription (). Hasmimetype (
Clipdescription.mimetype_text_urilist)) {
Here is the URI content www.jb51.net
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 ()) {
The data can be manipulated here, provided that you have permission
}
}
Pastecursor.close ();
}
}
}
}
}
}
Here is the value to receive the intent pass, a temporary activity, simpler code:
Copy Code code as follows:
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: Temporarily used to receive the intent value from clip.
* @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));
}
}
}
}
There is nothing in the global configuration file, as follows:
Copy Code code as follows:
<?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= "/>"
<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>
I hope this article will help you with your Android program.