龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android剪贴板用法详解

时间:2014-10-14 12:06来源:网络整理 作者:网络 点击:
分享到:
这篇文章主要介绍了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内容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()) {
                                  //此处对数据进行操作就可以了,前提是有权限
                             }
                         }
                         pasteCursor.close();
                      }
                  }
             }
         }
     }
 }

精彩图集

赞助商链接