W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
同一設(shè)備的應(yīng)用程序 A、B 之間可以借助系統(tǒng)剪貼板服務(wù)完成簡單數(shù)據(jù)的傳遞,即應(yīng)用程序 A 向剪貼板服務(wù)寫入數(shù)據(jù)后,應(yīng)用程序 B 可以從中讀取出數(shù)據(jù)。在滿足分布式剪貼板服務(wù)的使用條件時,應(yīng)用程序 A、B 也可以來自組網(wǎng)內(nèi)的不同設(shè)備。
圖1 剪貼板服務(wù)示意圖
在使用剪貼板服務(wù)時,需要注意以下幾點:
SystemPasteboard 提供系統(tǒng)剪貼板操作的相關(guān)接口,比如復(fù)制、粘貼、配置回調(diào)等。PasteData 是剪貼板服務(wù)操作的數(shù)據(jù)對象,一個 PasteData 由若干個內(nèi)容節(jié)點(PasteData.Record)和一個屬性集合對象(PasteData.DataProperty)組成。Record 是存放剪貼板數(shù)據(jù)內(nèi)容信息的最小單位,每個 Record 都有其特定的 MIME 類型,如純文本、HTML、URI、Intent。剪貼板數(shù)據(jù)的屬性信息存在放 DataProperty 中,包括標簽、時間戳、“僅在本地”標記位等。
SystemPasteboard
SystemPasteboard 提供系統(tǒng)剪貼板服務(wù)的操作接口,比如復(fù)制、粘貼、配置回調(diào)等。
接口名 | 描述 |
---|---|
getSystemPasteboard(Context context) | 獲取系統(tǒng)剪切板服務(wù)的對象實例。 |
getPasteData() | 讀取當前系統(tǒng)剪貼板中的數(shù)據(jù)。 |
hasPasteData() | 判斷當前系統(tǒng)剪貼板中是否有內(nèi)容。 |
setPasteData(PasteData data) | 將剪貼板數(shù)據(jù)寫入到系統(tǒng)剪貼板。 |
clear() | 清空系統(tǒng)剪貼板數(shù)據(jù)。 |
addPasteDataChangedListener(IPasteDataChangedListener listener) | 用戶程序添加系統(tǒng)剪貼板數(shù)據(jù)變化的回調(diào),當系統(tǒng)剪貼板數(shù)據(jù)發(fā)生變化時,會觸發(fā)用戶程序的回調(diào)實現(xiàn)。 |
removePasteDataChangedListener(IPasteDataChangedListener listener) | 用戶程序移除系統(tǒng)剪貼板數(shù)據(jù)變化的回調(diào)。 |
PasteData
PasteData 是剪貼板服務(wù)操作的數(shù)據(jù)對象,其中內(nèi)容節(jié)點定義為 PasteData.Record,屬性集合定義為 PasteData.DataProperty。
接口名 | 描述 |
---|---|
PasteData() | 構(gòu)造器,創(chuàng)建一個空內(nèi)容數(shù)據(jù)對象。 |
createPlainTextData(CharSequence text) | 構(gòu)建一個包含純文本內(nèi)容節(jié)點的數(shù)據(jù)對象。 |
creatHtmlData(String htmlText) | 構(gòu)建一個包含 HTML 內(nèi)容節(jié)點的數(shù)據(jù)對象。 |
creatUriData(Uri uri) | 構(gòu)建一個包含 URI 內(nèi)容節(jié)點的數(shù)據(jù)對象。 |
creatIntentData(Intent intent) | 構(gòu)建一個包含 Intent 內(nèi)容節(jié)點的數(shù)據(jù)對象。 |
getPrimaryMimeType() | 獲取數(shù)據(jù)對象中首個內(nèi)容節(jié)點的 MIME 類型,如果沒有查詢到內(nèi)容,將返回一個空字符串。 |
getPrimaryText() | 獲取數(shù)據(jù)對象中首個內(nèi)容節(jié)點的純文本內(nèi)容,如果沒有查詢到內(nèi)容,將返回一個空對象。 |
addTextRecord(CharSequence text) | 向數(shù)據(jù)對象中添加一個純文本內(nèi)容節(jié)點,該方法會自動更新數(shù)據(jù)屬性中的MIME類型集合,最多只能添加 128 個內(nèi)容節(jié)點。 |
addRecord(Record record) | 向數(shù)據(jù)對象中添加一個內(nèi)容節(jié)點,該方法會自動更新數(shù)據(jù)屬性中的 MIME 類型集合,最多只能添加 128 個內(nèi)容節(jié)點。 |
getRecordCount() | 獲取數(shù)據(jù)對象中內(nèi)容節(jié)點的數(shù)量。 |
getRecordAt(int index) | 獲取數(shù)據(jù)對象在指定下標處的內(nèi)容節(jié)點,如果操作失敗會返回空對象。 |
removeRecordAt(int index) | 移除數(shù)據(jù)對象在指定下標處的內(nèi)容節(jié)點,如果操作成功會返回 true,操作失敗會返回 false。 |
getMimeTypes() | 獲取數(shù)據(jù)對象中上所有內(nèi)容節(jié)點的MIME類型列表,當內(nèi)容節(jié)點為空時,返回列表為空對象。 |
getProperty() | 獲取該數(shù)據(jù)對象的屬性集合成員。 |
常量名 | 描述 |
---|---|
MIMETYPE_TEXT_PLAIN= "text/plain" | 純文本的 MIME 類型定義。 |
MIMETYPE_TEXT_HTML= "text/html" | HTML的 MIM E類型定義。 |
MIMETYPE_TEXT_URI= "text/uri" | URI的 MIME 類型定義。 |
MIMETYPE_TEXT_INTENT= "text/ohos.intent" | Intent的 MIME 類型定義。 |
MAX_RECORD_NUM=128 | 單個 PasteData 中所能包含的 Record 的數(shù)量上限。 |
PasteData.Record
一個 PasteData 中包含若干個特定MIME類型的 PasteData.Record,每個 Record 是存放剪貼板數(shù)據(jù)內(nèi)容信息的最小單位。
接口名 | 描述 |
---|---|
createPlainTextRecord(CharSequence text) | 構(gòu)造一個 MIME 類型為純文本的內(nèi)容節(jié)點。 |
createHtmlTextRecord(String htmlText) | 構(gòu)造一個 MIME 類型為HTML的內(nèi)容節(jié)點。 |
createUriRecord(Uri uri) | 構(gòu)造一個 MIME 類型為URI的內(nèi)容節(jié)點。 |
createIntentRecord(Intent intent) | 構(gòu)造一個 MIME 類型為Intent的內(nèi)容節(jié)點。 |
getPlainText() | 獲取該內(nèi)容節(jié)點中的文本內(nèi)容,如果沒有內(nèi)容將返回空對象。 |
getHtmlText() | 獲取該內(nèi)容節(jié)點中的 HTML 內(nèi)容,如果沒有內(nèi)容將返回空對象。 |
getUri() | 獲取該內(nèi)容節(jié)點中的 URI 內(nèi)容,如果沒有內(nèi)容將返回空對象。 |
getIntent() | 獲取該內(nèi)容節(jié)點中的 Intent 內(nèi)容,如果沒有內(nèi)容將返回空對象。 |
getMimeType() | 獲取該內(nèi)容節(jié)點的 MIME 類型。 |
convertToText(Context context) | 將該內(nèi)容節(jié)點的內(nèi)容轉(zhuǎn)為文本形式。 |
PasteData.DataProperty
每個 PasteData 中都有一個 PasteData.DataProperty 成員,其中存放著該數(shù)據(jù)對象的屬性集合,例如自定義標簽、MIME 類型集合列表,“僅在本地”標記位等。
接口名 | 描述 |
---|---|
getMimeTypes() | 獲取所屬數(shù)據(jù)對象的 MIME 類型集合列表,當內(nèi)容節(jié)點為空時,返回列表為空對象。 |
hasMimeType(String mimeType) | 判斷所屬數(shù)據(jù)對象中是否包含特定 MIME類型的內(nèi)容。 |
getTimestamp() | 獲取所屬數(shù)據(jù)對象被寫入系統(tǒng)剪貼板時的時間戳,如果該數(shù)據(jù)對象尚未被寫入,則返回0。 |
setTag(CharSequence tag) | 設(shè)置自定義標簽。 |
getTag() | 獲取自定義標簽。 |
setAdditions(PacMap extraProps) | 設(shè)置一些附加鍵值對信息。 |
getAdditions() | 獲取附加鍵值對信息。 |
setLocalOnly(boolean isLocalonly) | 配置“僅在本地”標志位,默認配置為false,表示此數(shù)據(jù)對象能在分布式剪貼板場景下跨設(shè)備傳遞,否則只在本地設(shè)備使用。 |
isLocalOnly() | 查詢“僅在本地”標志位。 |
IPasteDataChangedListener
IPasteDataChangedListener 是定義剪貼板數(shù)據(jù)變化回調(diào)的接口類,開發(fā)者需要實現(xiàn)此接口來編碼觸發(fā)回調(diào)時的處理邏輯。
接口名 | 描述 |
---|---|
onChanged() | 當系統(tǒng)剪貼板數(shù)據(jù)發(fā)生變化時的回調(diào)接口。 |
SystemPasteboard pasteboard = SystemPasteboard.getSystemPasteboard(appContext);
if (pasteboard != null) {
pasteboard.setPasteData(PasteData.creatPlainTextData("Hello, world!"));
}
PasteData pasteData = pasteboard.getPasteData();
if (pasteData == null) {
return;
}
DataProperty dataProperty = pasteData.getProperty();
boolean hasHtml = dataProperty.hasMimeType(PasteData.MIMETYPE_TEXT_HTML);
boolean hasText = dataProperty.hasMimeType(PasteData.MIMETYPE_TEXT_PLAIN);
if (hasHtml || hasText) {
for (int i = 0; i < pasteData.getRecordCount(); i++) {
Record record = pasteData.getRecordAt(i);
String mimeType = record.getMimeType();
if (mimeType.equals(PasteData.MIMETYPE_TEXT_HTML)) {
text.setText(record.getHtmlText());
break;
} else if (mimeType.equals(PasteData.MIMETYPE_TEXT_PLAIN)) {
text.setText(record.getPlainText().toString());
break;
}
}
}
IPasteDataChangedListener listener = new IPasteDataChangedListener() {
@Override
public void onChanged() {
PasteData pasteData = pasteboard.getPasteData();
if (pasteData == null) {
return;
}
// Operations to handle data change on the system pasteboard
}
};
pasteboard.addPasteDataChangedListener(listener);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: