Android clipboard usage in detail
- 2020-06-12 10:32:32
- OfStack
This article details the use of Android clipboard and shares it for your reference. The specific analysis is as follows:
First 1 point to note here, that is, when using Android clipboard everybody just remember 1 point, whether android devices or PC machine, copy and paste in the same time can only be used for 1 one object, the popular point is: PC machine, cannot simultaneously from C disk replication, and from D disk replication, specific look at the code, very simple, directly on the code:
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();
}
/**
* Initialize 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;
}
}
/**
* to Clip Put data in
*/
private void put(){
/**
* to ClipboardManager The available data types are 3 Kind of :
* Because everybody knows that , Even if it's a computer ,Ctrl+c It can't be the same 1 In that time
* from C Plate cutting and pasting , And from D A clip , So ponies just write 1 Kind of simple information in there ,
* The other two are in the comments
// type 1:text
clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData textCd = ClipData.newPlainText("kkk", "WaHouHou!Clip....");
clipboard.setPrimaryClip(textCd);
*/
/**
*
// type 2:URI
Uri copyUri = Uri.parse(CONTACTS + COPY_PATH + "/" + "XiaoMa");
ClipData clipUri = ClipData.newUri(getContentResolver(),"URI",copyUri);
clipboard.setPrimaryClip(clipUri);
*
*/
// type 3:Intent
// Try in Intent Cut and paste Bundle The value in
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);
}
/**
* from Clip In the data
*/
private void get(){
clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
Item item = null;
// Returns directly with no data
if(!clipboard.hasPrimaryClip()){
Toast.makeText(getApplicationContext(), " There is no data in the clipboard ", Toast.LENGTH_SHORT).show();
return ;
}
// If it's a text message
if (clipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_PLAIN)) {
ClipData cdText = clipboard.getPrimaryClip();
item = cdText.getItemAt(0);
// This is a TEXT Text information
if(item.getText() == null){
Toast.makeText(getApplicationContext(), " There is nothing in the clipboard ", Toast.LENGTH_SHORT).show();
return ;
}else{
Toast.makeText(getApplicationContext(), item.getText(), Toast.LENGTH_SHORT).show();
}
// If it is INTENT
} else if (clipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_INTENT)) {
// This is a INTENT
item = clipboard.getPrimaryClip().getItemAt(0);
Intent intent = item.getIntent();
startActivity(intent);
//........
// If it is URI
} else if (clipboard.getPrimaryClipDescription().hasMimeType(
ClipDescription.MIMETYPE_TEXT_URILIST)) {
// This is a URI content www.ofstack.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()) {
// This is where you can manipulate the data , The premise is that you have permissions
}
}
pasteCursor.close();
}
}
}
}
}
}
The following is the value used to receive the Intent pass, temporary 1 Activity, the code is simpler:
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: Used temporarily to receive the lower slave Clip To get the Intent value
* @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:
<?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>
Hopefully, this article has helped you with your Android programming.