本模塊主要提供管理系統(tǒng)剪貼板的能力,為系統(tǒng)復制、粘貼功能提供支持。系統(tǒng)剪貼板支持對文本、HTML、URI、Want等內(nèi)容的操作。
本模塊首批接口從API version 6開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
名稱 | 類型 | 值 | 說明 |
---|---|---|---|
MAX_RECORD_NUM7+ | number | 512 | 單個PasteData中所能包含的最大條目數(shù)。 |
MIMETYPE_TEXT_HTML7+ | string | 'text/html' | HTML內(nèi)容的MIME類型定義。 |
MIMETYPE_TEXT_WANT7+ | string | 'text/want' | Want內(nèi)容的MIME類型定義。 |
MIMETYPE_TEXT_PLAIN7+ | string | 'text/plain' | 純文本內(nèi)容的MIME類型定義。 |
MIMETYPE_TEXT_URI7+ | string | 'text/uri' | URI內(nèi)容的MIME類型定義。 |
MIMETYPE_PIXELMAP9+ | string | 'pixelMap' | PixelMap內(nèi)容的MIME類型定義。 |
用于表示允許的數(shù)據(jù)字段類型。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
類型 | 說明 |
---|---|
string | 表示string的類型。 |
image.PixelMap | 表示image.PixelMap的類型。 |
Want | 表示Want的類型。 |
ArrayBuffer | 表示ArrayBuffer的類型。 |
createData(mimeType: string, value: ValueType): PasteData
構建一個自定義類型的剪貼板內(nèi)容對象。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
mimeType | string | 是 | 自定義數(shù)據(jù)的MIME類型。 |
value | 是 | 自定義數(shù)據(jù)內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
剪貼板內(nèi)容對象。 |
示例:
- let dataXml = 'Hello World';
- let pasteData = pasteboard.createData('app/xml', dataXml);
createRecord(mimeType: string, value: ValueType):PasteDataRecord;
創(chuàng)建一條自定義數(shù)據(jù)內(nèi)容條目。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
mimeType | string | 是 | 自定義數(shù)據(jù)的MIME類型。 |
value | 是 | 自定義數(shù)據(jù)內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
一條新建的自定義數(shù)據(jù)內(nèi)容條目。 |
示例:
- let dataXml = 'Hello World';
- let pasteDataRecord = pasteboard.createRecord('app/xml', dataXml);
getSystemPasteboard(): SystemPasteboard
獲取系統(tǒng)剪貼板對象。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
系統(tǒng)剪貼板對象。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
可粘貼數(shù)據(jù)的范圍類型枚舉。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
名稱 | 值 | 說明 |
---|---|---|
INAPP | 0 | 表示僅允許同應用內(nèi)粘貼。 |
LOCALDEVICE | 1 | 表示允許在此設備中任何應用內(nèi)粘貼。 |
CROSSDEVICE | 2 | 表示允許跨設備在任何應用內(nèi)粘貼。 |
createHtmlData(htmlText: string): PasteData
構建一個HTML剪貼板內(nèi)容對象。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
htmlText | string | 是 | HTML內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
剪貼板內(nèi)容對象。 |
示例:
- let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>";
- let pasteData = pasteboard.createHtmlData(html);
createWantData(want: Want): PasteData
構建一個Want剪貼板內(nèi)容對象。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | 是 | Want內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
剪貼板內(nèi)容對象。 |
示例:
- let object = {
- bundleName: "com.example.aafwk.test",
- abilityName: "com.example.aafwk.test.TwoAbility"
- };
- let pasteData = pasteboard.createWantData(object);
createPlainTextData(text: string): PasteData
構建一個純文本剪貼板內(nèi)容對象。
從 API Version 6 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | 純文本內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
剪貼板內(nèi)容對象。 |
示例:
- let pasteData = pasteboard.createPlainTextData('content');
createUriData(uri: string): PasteData
構建一個URI剪貼板內(nèi)容對象。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
uri | string | 是 | URI內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
剪貼板內(nèi)容對象。 |
示例:
- let pasteData = pasteboard.createUriData('dataability:///com.example.myapplication1/user.txt');
createHtmlTextRecord(htmlText: string): PasteDataRecord
創(chuàng)建一條HTML內(nèi)容的條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
htmlText | string | 是 | HTML內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
一條新建的HTML內(nèi)容條目。 |
示例:
- let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>";
- let record = pasteboard.createHtmlTextRecord(html);
createWantRecord(want: Want): PasteDataRecord
創(chuàng)建一條Want內(nèi)容條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | 是 | Want內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
一條新建的Want內(nèi)容條目。 |
示例:
- let object = {
- bundleName: "com.example.aafwk.test",
- abilityName: "com.example.aafwk.test.TwoAbility"
- };
- let record = pasteboard.createWantRecord(object);
createPlainTextRecord(text: string): PasteDataRecord
創(chuàng)建一條純文本內(nèi)容條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | 純文本內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
一條新建的純文本內(nèi)容條目。 |
示例:
- let record = pasteboard.createPlainTextRecord('hello');
createUriRecord(uri: string): PasteDataRecord
創(chuàng)建一條URI內(nèi)容的條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
uri | string | 是 | URI內(nèi)容。 |
返回值:
類型 | 說明 |
---|---|
一條新建的URI內(nèi)容條目。 |
示例:
- let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
定義了剪貼板中所有內(nèi)容條目的屬性,包含時間戳、數(shù)據(jù)類型、粘貼范圍以及一些附加數(shù)據(jù)等。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
additions7+ | {[key:string]:object} | 是 | 是 | 設置的其他附加屬性數(shù)據(jù),暫不支持。 |
mimeTypes7+ | Array<string> | 是 | 否 | 剪貼板內(nèi)容條目的數(shù)據(jù)類型,非重復的類型列表。 |
tag7+ | string | 是 | 是 | 用戶自定義標簽,暫不支持。 |
timestamp7+ | number | 是 | 否 | 剪貼板數(shù)據(jù)的寫入時間戳(單位:ms)。 |
localOnly7+ | boolean | 是 | 是 | 配置剪貼板內(nèi)容是否為“僅在本地”,默認值為true。暫不支持,推薦使用shareOption屬性。 - 配置為true時,表示內(nèi)容僅在本地,不會在設備之間傳遞。 - 配置為false時,表示內(nèi)容將在設備間傳遞。 |
shareOption9+ | 是 | 是 | 指示剪貼板數(shù)據(jù)可以粘貼到的范圍,如果未設置或設置不正確,則默認值為CROSSDEVICE。 |
對于剪貼板中內(nèi)容記錄的抽象定義,稱之為條目。剪貼板內(nèi)容部分由一個或者多個條目構成,例如一條文本內(nèi)容、一份HTML、一個URI或者一個Want。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
名稱 | 類型 | 可讀 | 可寫 | 說明 |
---|---|---|---|---|
htmlText7+ | string | 是 | 否 | HTML內(nèi)容。 |
want7+ | 是 | 否 | Want內(nèi)容。 | |
mimeType7+ | string | 是 | 否 | 數(shù)據(jù)類型。 |
plainText7+ | string | 是 | 否 | 純文本內(nèi)容。 |
uri7+ | string | 是 | 否 | URI內(nèi)容。 |
pixelMap9+ | 是 | 否 | PixelMap內(nèi)容。 | |
data9+ | {[mimeType: string]: ArrayBuffer} | 是 | 否 | 自定義數(shù)據(jù)內(nèi)容。 |
toPlainText(): string
將一個PasteData中的內(nèi)容強制轉(zhuǎn)換為文本內(nèi)容。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
string | 純文本內(nèi)容。 |
示例:
- let record = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
- let data = record.toPlainText();
- console.info(`Succeeded in converting to text. Data: ${data}`);
convertToText(callback: AsyncCallback<string>): void
將一個PasteData中的內(nèi)容強制轉(zhuǎn)換為文本內(nèi)容,使用callback異步回調(diào)。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用toPlainText替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<string> | 是 | 回調(diào)函數(shù),當轉(zhuǎn)換成功,err為undefined,data為強制轉(zhuǎn)換的文本內(nèi)容;否則返回錯誤信息。 |
示例:
- let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
- record.convertToText((err, data) => {
- if (err) {
- console.error(`Failed to convert to text. Cause: ${err.message}`);
- return;
- }
- console.info(`Succeeded in converting to text. Data: ${data}`);
- });
convertToText(): Promise<string>
將一個PasteData中的內(nèi)容強制轉(zhuǎn)換為文本內(nèi)容,使用Promise異步回調(diào)。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用toPlainText替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<string> | Promise對象,返回強制轉(zhuǎn)換的文本內(nèi)容。 |
示例:
- let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
- record.convertToText().then((data) => {
- console.info(`Succeeded in converting to text. Data: ${data}`);
- }).catch((err) => {
- console.error(`Failed to convert to text. Cause: ${err.message}`);
- });
剪貼板內(nèi)容對象。剪貼板內(nèi)容包含一個或者多個內(nèi)容條目(PasteDataRecord)以及屬性描述對象(PasteDataProperty)。
在調(diào)用PasteData的接口前,需要先通過createData()或getData()獲取一個PasteData對象。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
getPrimaryText(): string
獲取首個條目的純文本內(nèi)容。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
string | 純文本內(nèi)容。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let plainText = pasteData.getPrimaryText();
getPrimaryHtml(): string
獲取首個條目的HTML內(nèi)容。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
string | HTML內(nèi)容。 |
示例:
- let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>";
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_HTML, html);
- let htmlText = pasteData.getPrimaryHtml();
getPrimaryWant(): Want
獲取首個條目的Want對象內(nèi)容。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Want對象內(nèi)容。 |
示例:
- let object = {
- bundleName: "com.example.aafwk.test",
- abilityName: "com.example.aafwk.test.TwoAbility"
- };
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_WANT, object);
- let want = pasteData.getPrimaryWant();
getPrimaryUri(): string
獲取首個條目的URI內(nèi)容。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
string | URI內(nèi)容。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
- let uri = pasteData.getPrimaryUri();
getPrimaryPixelMap(): image.PixelMap
獲取首個條目的PixelMap內(nèi)容。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
PixelMap內(nèi)容。 |
示例:
- import image from '@ohos.multimedia.image';
- let buffer = new ArrayBuffer(128);
- let opt = {
- size: { height: 3, width: 5 },
- pixelFormat: 3,
- editable: true,
- alphaType: 1,
- scaleMode: 1
- };
- image.createPixelMap(buffer, opt).then((pixelMap) => {
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_PIXELMAP, pixelMap);
- let PixelMap = pasteData.getPrimaryPixelMap();
- });
addRecord(record: PasteDataRecord): void
向當前剪貼板內(nèi)容中添加一條條目,同時也會將條目類型添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭眨駝t添加失敗。
剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
record | 是 | 待添加的條目。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
- let textRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>";
- let htmlRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_HTML, html);
- pasteData.addRecord(textRecord);
- pasteData.addRecord(htmlRecord);
addRecord(mimeType: string, value: ValueType): void
向當前剪貼板內(nèi)容中添加一條自定義數(shù)據(jù)內(nèi)容條目,同時也會將自定義數(shù)據(jù)類型添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭眨駝t添加失敗。
剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
mimeType | string | 是 | 自定義數(shù)據(jù)的MIME類型。 |
value | 是 | 自定義數(shù)據(jù)內(nèi)容。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900002 | The number of record exceeds the maximum limit. |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
- let dataXml = 'Hello World';
- pasteData.addRecord('app/xml', dataXml);
getMimeTypes(): Array<string>
獲取剪貼板中PasteDataProperty的mimeTypes列表,當剪貼板內(nèi)容為空時,返回列表為空。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Array<string> | 剪貼板內(nèi)容條目的數(shù)據(jù)類型,非重復的類型列表。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let types = pasteData.getMimeTypes();
getPrimaryMimeType(): string
獲取剪貼板內(nèi)容中首個條目的數(shù)據(jù)類型。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
string | 首個條目的數(shù)據(jù)類型。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let type = pasteData.getPrimaryMimeType();
getProperty(): PasteDataProperty
獲取剪貼板內(nèi)容的屬性描述對象。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
屬性描述對象。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let property = pasteData.getProperty();
setProperty(property: PasteDataProperty): void
設置剪貼板內(nèi)容的屬性描述對象,當前僅支持設置shareOption屬性。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
property | 是 | 屬性描述對象。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_HTML, 'application/xml');
- let prop = pasteData.getProperty();
- prop.shareOption = pasteboard.ShareOption.INAPP;
- pasteData.setProperty(prop);
getRecord(index: number): PasteDataRecord
獲取剪貼板內(nèi)容中指定下標的條目。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 指定條目的下標。 |
返回值:
類型 | 說明 |
---|---|
指定下標的條目。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900001 | The index is out of the record. |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let record = pasteData.getRecord(0);
getRecordCount(): number
獲取剪貼板內(nèi)容中條目的個數(shù)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
number | 條目的個數(shù)。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let count = pasteData.getRecordCount();
getTag(): string
獲取剪貼板內(nèi)容中用戶自定義的標簽內(nèi)容,如果沒有設置用戶自定義的標簽內(nèi)容將返回空。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
string | 返回用戶自定義的標簽內(nèi)容,如果沒有設置用戶自定義的標簽內(nèi)容,將返回空。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let tag = pasteData.getTag();
hasType(mimeType: string): boolean
檢查剪貼板內(nèi)容中是否有指定的MIME數(shù)據(jù)類型。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
mimeType | string | 是 | 待查詢的數(shù)據(jù)類型。 |
返回值:
類型 | 說明 |
---|---|
boolean | 有指定的數(shù)據(jù)類型返回true,否則返回false。 |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let hasType = pasteData.hasType(pasteboard.MIMETYPE_TEXT_PLAIN);
removeRecord(index: number): void
移除剪貼板內(nèi)容中指定下標的條目。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 指定的下標。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900001 | The index is out of the record. |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- pasteData.removeRecord(0);
replaceRecord(index: number, record: PasteDataRecord): void
替換剪貼板內(nèi)容中指定下標的條目。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 指定的下標。 |
record | 是 | 新條目的內(nèi)容。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900001 | The index is out of the record. |
示例:
- let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
- let record = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
- pasteData.replaceRecord(0, record);
addHtmlRecord(htmlText: string): void
向當前剪貼板內(nèi)容中添加一條HTML內(nèi)容條目,并將MIMETYPE_TEXT_HTML添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭眨駝t添加失敗。
剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用addRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
htmlText | string | 是 | HTML內(nèi)容。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- let html = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "<meta charset=\"utf-8\">\n" + "<title>HTML-PASTEBOARD_HTML</title>\n" + "</head>\n" + "<body>\n" + " <h1>HEAD</h1>\n" + " <p></p>\n" + "</body>\n" + "</html>";
- pasteData.addHtmlRecord(html);
addWantRecord(want: Want): void
向當前剪貼板內(nèi)容中添加一條Want條目,并將MIMETYPE_TEXT_WANT添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭?,否則添加失敗。
剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用addRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
want | 是 | Want對象內(nèi)容。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- let object = {
- bundleName: "com.example.aafwk.test",
- abilityName: "com.example.aafwk.test.TwoAbility"
- };
- pasteData.addWantRecord(object);
addTextRecord(text: string): void
向當前剪貼板內(nèi)容中添加一條純文本條目,并將MIME_TEXT_PLAIN添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭眨駝t添加失敗。
剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用addRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
text | string | 是 | 純文本內(nèi)容。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- pasteData.addTextRecord('good');
addUriRecord(uri: string): void
向當前剪貼板內(nèi)容中添加一條URI條目,并將MIMETYPE_TEXT_URI添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭?,否則添加失敗。
剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用addRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
uri | string | 是 | URI內(nèi)容。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- pasteData.addUriRecord('dataability:///com.example.myapplication1/user.txt');
getRecordAt(index: number): PasteDataRecord
獲取剪貼板內(nèi)容中指定下標的條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用getRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 指定條目的下標。 |
返回值:
類型 | 說明 |
---|---|
指定下標的條目。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- let record = pasteData.getRecordAt(0);
hasMimeType(mimeType: string): boolean
檢查剪貼板內(nèi)容中是否有指定的數(shù)據(jù)類型。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用hasType替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
mimeType | string | 是 | 待查詢的數(shù)據(jù)類型。 |
返回值:
類型 | 說明 |
---|---|
boolean | 有指定的數(shù)據(jù)類型返回true,否則返回false。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- let hasType = pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_PLAIN);
removeRecordAt(index: number): boolean
移除剪貼板內(nèi)容中指定下標的條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用removeRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 指定的下標。 |
返回值:
類型 | 說明 |
---|---|
boolean | 成功移除返回true,失敗返回false。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- let isRemove = pasteData.removeRecordAt(0);
replaceRecordAt(index: number, record: PasteDataRecord): boolean
替換剪貼板內(nèi)容中指定下標的條目。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用replaceRecord替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
index | number | 是 | 指定的下標。 |
record | 是 | 替換后的條目。 |
返回值:
類型 | 說明 |
---|---|
boolean | 成功替換返回true,失敗返回false。 |
示例:
- let pasteData = pasteboard.createPlainTextData('hello');
- let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
- let isReplace = pasteData.replaceRecordAt(0, record);
系統(tǒng)剪貼板對象。
在調(diào)用SystemPasteboard的接口前,需要先通過getSystemPasteboard獲取系統(tǒng)剪貼板。
- let systemPasteboard = pasteboard.getSystemPasteboard();
on(type: 'update', callback: () =>void ): void
訂閱系統(tǒng)剪貼板內(nèi)容變化事件,當系統(tǒng)剪貼板中內(nèi)容變化時觸發(fā)用戶程序的回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
type | string | 是 | 取值為'update',表示系統(tǒng)剪貼板內(nèi)容變化事件。 |
callback | function | 是 | 剪貼板中內(nèi)容變化時觸發(fā)的用戶程序的回調(diào)。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- let listener = () => {
- console.info('The system pasteboard has changed.');
- };
- systemPasteboard.on('update', listener);
off(type: 'update', callback?: () =>void ): void
取消訂閱系統(tǒng)剪貼板內(nèi)容變化事件。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
type | string | 是 | 取值為'update',表示系統(tǒng)剪貼板內(nèi)容變化事件。 |
callback | function | 否 | 剪貼板中內(nèi)容變化時觸發(fā)的用戶程序的回調(diào)。如果此參數(shù)未填,表明清除本應用的所有監(jiān)聽回調(diào),否則表示清除指定監(jiān)聽回調(diào)。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- let listener = () => {
- console.info('The system pasteboard has changed.');
- };
- systemPasteboard.off('update', listener);
clearData(callback: AsyncCallback<void>): void
清空系統(tǒng)剪貼板內(nèi)容,使用callback異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù)。當成功清空時,err為undefined;否則為錯誤對象。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.clearData((err, data) => {
- if (err) {
- console.error(`Failed to clear the pasteboard. Cause: ${err.message}`);
- return;
- }
- console.info('Succeeded in clearing the pasteboard.');
- });
clearData(): Promise<void>
清空系統(tǒng)剪貼板內(nèi)容,使用Promise異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<void> | 無返回結果的Promise對象。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.clearData().then((data) => {
- console.info('Succeeded in clearing the pasteboard.');
- }).catch((err) => {
- console.error(`Failed to clear the pasteboard. Cause: ${err.message}`);
- });
setData(data: PasteData, callback: AsyncCallback<void>): void
將數(shù)據(jù)寫入系統(tǒng)剪貼板,使用callback異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | PasteData對象。 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù)。當寫入成功,err為undefined,否則為錯誤對象。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900003 | Another copy or paste is in progress. |
12900004 | Replication is prohibited. |
示例:
- let pasteData = pasteboard.createPlainTextData('content');
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.setData(pasteData, (err, data) => {
- if (err) {
- console.error('Failed to set PasteData. Cause: ' + err.message);
- return;
- }
- console.info('Succeeded in setting PasteData.');
- });
setData(data: PasteData): Promise<void>
將數(shù)據(jù)寫入系統(tǒng)剪貼板,使用Promise異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | PasteData對象。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | 無返回結果的Promise對象。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900003 | Another copy or paste is in progress. |
12900004 | Replication is prohibited. |
示例:
- let pasteData = pasteboard.createPlainTextData('content');
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.setData(pasteData).then((data) => {
- console.info('Succeeded in setting PasteData.');
- }).catch((err) => {
- console.error('Failed to set PasteData. Cause: ' + err.message);
- });
getData( callback: AsyncCallback<PasteData>): void
讀取系統(tǒng)剪貼板內(nèi)容,使用callback異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<PasteData> | 是 | 回調(diào)函數(shù)。當讀取成功,err為undefined,data為返回的系統(tǒng)剪貼板數(shù)據(jù);否則返回錯誤對象。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900003 | Another copy or paste is in progress. |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.getData((err, pasteData) => {
- if (err) {
- console.error('Failed to get PasteData. Cause: ' + err.message);
- return;
- }
- let text = pasteData.getPrimaryText();
- });
getData(): Promise<PasteData>
讀取系統(tǒng)剪貼板內(nèi)容,使用Promise異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<PasteData> | Promise對象,返回系統(tǒng)剪貼板數(shù)據(jù)。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。
錯誤碼ID | 錯誤信息 |
---|---|
12900003 | Another copy or paste is in progress. |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.getData().then((pasteData) => {
- let text = pasteData.getPrimaryText();
- }).catch((err) => {
- console.error('Failed to get PasteData. Cause: ' + err.message);
- })
hasData(callback: AsyncCallback<boolean>): void
判斷系統(tǒng)剪貼板中是否有內(nèi)容,使用callback異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<boolean> | 是 | 返回true表示系統(tǒng)剪貼板中有內(nèi)容,返回false表示系統(tǒng)剪貼板中沒有內(nèi)容。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.hasData((err, data) => {
- if (err) {
- console.error(`Failed to check the PasteData. Cause: ${err.message}`);
- return;
- }
- console.info(`Succeeded in checking the PasteData. Data: ${data}`);
- });
hasData(): Promise<boolean>
判斷系統(tǒng)剪貼板中是否有內(nèi)容,使用Promise異步回調(diào)。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<boolean> | 返回true表示系統(tǒng)剪貼板中有內(nèi)容,返回false表示系統(tǒng)剪貼板中沒有內(nèi)容。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.hasData().then((data) => {
- console.info(`Succeeded in checking the PasteData. Data: ${data}`);
- }).catch((err) => {
- console.error(`Failed to check the PasteData. Cause: ${err.message}`);
- });
clear(callback: AsyncCallback<void>): void
清空系統(tǒng)剪貼板內(nèi)容,使用callback異步回調(diào)。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.clearData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù)。當成功清空時,err為undefined;否則為錯誤對象。 |
示例:
- systemPasteboard.clear((err, data) => {
- if (err) {
- console.error(`Failed to clear the PasteData. Cause: ${err.message}`);
- return;
- }
- console.info('Succeeded in clearing the PasteData.');
- });
clear(): Promise<void>
清空系統(tǒng)剪貼板內(nèi)容,使用Promise異步回調(diào)。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.clearData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<void> | 無返回結果的Promise對象。 |
示例:
- systemPasteboard.clear().then((data) => {
- console.info('Succeeded in clearing the PasteData.');
- }).catch((err) => {
- console.error(`Failed to clear the PasteData. Cause: ${err.message}`);
- });
getPasteData( callback: AsyncCallback<PasteData>): void
讀取系統(tǒng)剪貼板內(nèi)容,使用callback異步回調(diào)。
從 API Version 6 開始支持,從 API Version 9 開始廢棄,建議使用getData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<PasteData> | 是 | 回調(diào)函數(shù)。當讀取成功,err為undefined,data為返回的系統(tǒng)剪貼板數(shù)據(jù);否則返回錯誤對象。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.getPasteData((err, pasteData) => {
- if (err) {
- console.error('Failed to get PasteData. Cause: ' + err.message);
- return;
- }
- let text = pasteData.getPrimaryText();
- });
getPasteData(): Promise<PasteData>
讀取系統(tǒng)剪貼板內(nèi)容,使用Promise異步回調(diào)。
從 API Version 6 開始支持,從 API Version 9 開始廢棄,建議使用getData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<PasteData> | Promise對象,返回系統(tǒng)剪貼板數(shù)據(jù)。 |
示例:
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.getPasteData().then((pasteData) => {
- let text = pasteData.getPrimaryText();
- }).catch((err) => {
- console.error('Failed to get PasteData. Cause: ' + err.message);
- })
hasPasteData(callback: AsyncCallback<boolean>): void
判斷系統(tǒng)剪貼板中是否有內(nèi)容,使用callback異步回調(diào)。
從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用hasData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
callback | AsyncCallback<boolean> | 是 | 返回true表示系統(tǒng)剪貼板中有內(nèi)容,返回false表示系統(tǒng)剪貼板中沒有內(nèi)容。 |
示例:
- systemPasteboard.hasPasteData((err, data) => {
- if (err) {
- console.error(`Failed to check the PasteData. Cause: ${err.message}`);
- return;
- }
- console.info(`Succeeded in checking the PasteData. Data: ${data}`);
- });
hasPasteData(): Promise<boolean>
判斷系統(tǒng)剪貼板中是否有內(nèi)容,使用Promise異步回調(diào)。
從 API Version 6 開始支持,從 API Version 9 開始廢棄,建議使用hasData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
返回值:
類型 | 說明 |
---|---|
Promise<boolean> | 返回true表示系統(tǒng)剪貼板中有內(nèi)容,返回false表示系統(tǒng)剪貼板中沒有內(nèi)容。 |
示例:
- systemPasteboard.hasPasteData().then((data) => {
- console.info(`Succeeded in checking the PasteData. Data: ${data}`);
- }).catch((err) => {
- console.error(`Failed to check the PasteData. Cause: ${err.message}`);
- });
setPasteData(data: PasteData, callback: AsyncCallback<void>): void
將數(shù)據(jù)寫入系統(tǒng)剪貼板,使用callback異步回調(diào)。
從 API Version 6 開始支持,從 API Version 9 開始廢棄,建議使用setData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | PasteData對象。 | |
callback | AsyncCallback<void> | 是 | 回調(diào)函數(shù)。當寫入成功,err為undefined,否則為錯誤對象。 |
示例:
- let pasteData = pasteboard.createPlainTextData('content');
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.setPasteData(pasteData, (err, data) => {
- if (err) {
- console.error('Failed to set PasteData. Cause: ' + err.message);
- return;
- }
- console.info('Succeeded in setting PasteData.');
- });
setPasteData(data: PasteData): Promise<void>
將數(shù)據(jù)寫入系統(tǒng)剪貼板,使用Promise異步回調(diào)。
從 API Version 5 開始支持,從 API Version 9 開始廢棄,建議使用setData替代。
系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard
參數(shù):
參數(shù)名 | 類型 | 必填 | 說明 |
---|---|---|---|
data | 是 | PasteData對象。 |
返回值:
類型 | 說明 |
---|---|
Promise<void> | 無返回結果的Promise對象。 |
示例:
- let pasteData = pasteboard.createPlainTextData('content');
- let systemPasteboard = pasteboard.getSystemPasteboard();
- systemPasteboard.setPasteData(pasteData).then((data) => {
- console.info('Succeeded in setting PasteData.');
- }).catch((err) => {
- console.error('Failed to set PasteData. Cause: ' + err.message);
- });
更多建議: