安卓全局的复制粘贴--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服务:

1
2
3
// Gets a handle to the clipboard service.
ClipboardManager clipboardmanager = (ClipboardManager)       
getSystemService(Context.CLIPBOARD_SERVICE);

###复制:

复制text:

1
2
3
// Creates a new text clip to put on the
clipboardClipData clip = ClipData.newPlainText("simple text","Hello, World!");
clipboardmanager.setPrimaryClip(clip);

复制URL:

1
2
3
4
5
6
7
8
9
10
11
12
// 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:

1
2
3
4
5
6
// 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);

###粘贴:

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

1
2
3
4
5
6
7
8
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这样当我们获取到剪切板的数据后就可以做各种事情比如获取到用户复制了英文单词则可以帮助翻译,复制了链接则帮助打开等等。
监听剪切板变化的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
}
}
});

Contents
  1. 1. #安卓全局Clipboard
  2. 2. ##HOW TO USE:
  3. 3. ###复制:
  4. 4. ###粘贴:
|