安卓全局的复制粘贴 Clipboard

#安卓全局Clipboard

Clipboard官方文档在此(需要科学上网...) Clioboard 的官方解释是这样的:


Android provides a powerful clipboard-based framework for copying and pasting. It supports both simple and complex data types, including text strings, complex data structures, text and binary stream data, and even application assets


大概意思就是: Android提供了一个强大的复制和粘贴clipboard-based框架。它同时支持的简单和复杂数据类型,包括文本字符串,复杂的数据结构,文本和二进制数据流,甚至应用程序资源. Clipboard支持三种形式的数据传递:text,URL,Intent.

某宝的口令应该就是之类的方式来实现的。 ##HOW TO USE:

直接上官方的示例代码 ###获取Clipboard服务:

// Gets a handle to the clipboard service.
ClipboardManager clipboardmanager = (ClipboardManager)       
getSystemService(Context.CLIPBOARD_SERVICE);
复制代码

###复制:

复制text:

// Creates a new text clip to put on the 
clipboardClipData clip = ClipData.newPlainText("simple text","Hello, World!");
clipboardmanager.setPrimaryClip(clip);
复制代码

复制URL:

// Creates a Uri based on a base Uri and a record ID based on the contact's last name
// Declares the base URI string
private static final String CONTACTS = "content://com.example.contacts";
// Declares a path string for URIs that you use to copy data
private static final String COPY_PATH = "/copy";// Declares the Uri to paste to the clipboard
Uri copyUri = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName);
...
// Creates a new URI clip object. The system uses the anonymous getContentResolver() object to
// get MIME types from provider. The clip object's label is "URI", and its data is
// the Uri previously created.
ClipData clip = ClipData.newUri(getContentResolver(),"URI",copyUri);
clipboardmanager.setPrimaryClip(clip);
复制代码

复制Intent:

// Creates the IntentIntent appIntent = new Intent(this, com.example.demo.myapplication.class);
...
// Creates a clip object with the Intent in it. Its label is "Intent" and its data is
// the Intent object created previously
  ClipData clip = ClipData.newIntent("Intent",appIntent);
clipboardmanager.setPrimaryClip(clip);
复制代码

###粘贴:

获取的方式都差不多 获取:

ClipData clipData = mClipboardManager.getPrimaryClip();
ClipData.Item item = clipData.getItemAt(0);
//获取text
String text = item.getText().toString();
//获取uri
Uri pasteUri = item.getUri();
//获取intent:
Intent intent = item.getIntent();
复制代码

google还为我们提供了剪切板发生改变的PrimaryClipChangedListener这样当我们获取到剪切板的数据后就可以做各种事情比如获取到用户复制了英文单词则可以帮助翻译,复制了链接则帮助打开等等。 监听剪切板变化的代码如下:

mClipboardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
    @Override
    public void onPrimaryClipChanged() {
      if (clipboardManager.hasPrimaryClip()){    
          Toast.makeText(MainActivity.this, "剪切板发生改变" ,Toast.LENGTH_SHORT).show();    
    ClipData clipData = clipboardManager.getPrimaryClip();    
      ClipData.Item item = clipData.getItemAt(0);   
         String text = item.getText().toString();    
        Toast.makeText(MainActivity.this, "获取到的剪切板文字为:"+text,       Toast.LENGTH_SHORT).show();}
      else {    
        Toast.makeText(MainActivity.this, "剪切板上没有数据", Toast.LENGTH_SHORT).show();
          }
    }
});
复制代码

转载于:https://juejin.im/post/5a3396db518825552b3f99ab

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值