看見google的介紹粘貼板的部分,記錄一下
原文: https://developer.android.com/guide/topics/text/copy-paste.html#Clipboard
github:https://github.com/CL-window/Clicpboard,做了個小例子
支持三種格式
Text String
URI 一個URI
Intent 一個Intent
使用 ,主要用到 ClipboardManager
https://developer.android.com/reference/android/content/ClipboardManager.html
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);複製數據至 ClipData:
For text:
ClipData clip = ClipData.newPlainText(label, text);
For a URI:
ClipData clip = ClipData.newUri(resolver, label, URI);
For an Intent:
ClipData clip = ClipData.newIntent(label, intent);// intent = new Intent(this, xxx.class)
將ClipData賦值給clipboard
clipboard.setPrimaryClip(clip);
從 clipboard 得到數據
ClipboardManager中有很多判斷與操作方法:
getPrimaryClip() 返回剪貼板上的當前Copy內容
getPrimaryClipDescription() 返回剪貼板上的當前Copy的說明
hasPrimaryClip() 如果當前剪貼板上存在Copy返回True
setPrimaryClip(ClipData clip) 設置剪貼板上的當前Copy
setText(CharSequence text) 設置文本到當前Copy
getText() 獲取剪貼板複製的文本
Text:
第一步,得到全局的ClipboardManager
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
第二步,開啓或關閉"paste"選項,判斷clipboard是否有ClipData
// Gets the ID of the "paste" menu item
MenuItem mPasteItem = menu.findItem(R.id.menu_paste);
// If the clipboard doesn't contain data, disable the paste menu item.
// If it does contain data, decide if you can handle the data.
if (!(clipboard.hasPrimaryClip())) {
mPasteItem.setEnabled(false);
} else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {
// This disables the paste menu item, since the clipboard has data but it is not plain text
mPasteItem.setEnabled(false);
} else {
// This enables the paste menu item, since the clipboard contains plain text.
mPasteItem.setEnabled(true);
}
第三步,複製數據
// Examines the item on the clipboard. If getText() does not return null, the clip item contains the
// text. Assumes that this application can only handle one item at a time.
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
// Gets the clipboard as text.
pasteData = item.getText();
// If the string contains data, then the paste operation is done
if (pasteData != null) {
return;
// The clipboard does not contain text. If it contains a URI, attempts to get data from it
} else {
Uri pasteUri = item.getUri();
// If the URI contains something, try to get text from it
if (pasteUri != null) {
// calls a routine to resolve the URI and get data from it. This routine is not
// presented here.
pasteData = resolveUri(Uri);
return;
} else {
// Something is wrong. The MIME type was plain text, but the clipboard does not contain either
// text or a Uri. Report an error.
Log.e("Clipboard contains an invalid data type");
return;
}
}
URI:步驟類似
ClipData clip = clipboard.getPrimaryClip();
if (clip != null) {
// Gets the first item from the clipboard data
ClipData.Item item = clip.getItemAt(0);
// Tries to get the item's contents as a URI
Uri pasteUri = item.getUri();
// If the clipboard contains a URI reference
if (pasteUri != null) {
// Is this a content URI?
String uriMimeType = cr.getType(pasteUri);
// If the return value is not null, the Uri is a content Uri
if (uriMimeType != null) {
// Does the content provider offer a MIME type that the current application can use?
if (uriMimeType.equals(MIME_TYPE_CONTACT)) {
// Get the data from the content provider.
Cursor pasteCursor = cr.query(uri, null, null, null, null);
// If the Cursor contains data, move to the first record
if (pasteCursor != null) {
if (pasteCursor.moveToFirst()) {
// get the data from the Cursor here. The code will vary according to the
// format of the data model.
}
}
// close the Cursor
pasteCursor.close();
}
}
}
}
Intent:
// Checks to see if the clip item contains an Intent, by testing to see if getIntent() returns null
Intent pasteIntent = clipboard.getPrimaryClip().getItemAt(0).getIntent();
if (pasteIntent != null) {
// handle the Intent
} else {
// ignore the clipboard, or issue an error if your application was expecting an Intent to be
// on the clipboard
}
一路看下來,好像是一路判斷就行,就三種,得到的數據判斷一下是否爲空,就是這種數據