剪貼板

2024-01-23 17:25 更新

本模塊主要提供管理系統(tǒng)剪貼板的能力,為系統(tǒng)復制、粘貼功能提供支持。系統(tǒng)剪貼板支持對文本、HTML、URI、Want等內(nèi)容的操作。

說明

本模塊首批接口從API version 6開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

  1. import pasteboard from '@ohos.pasteboard';

常量

系統(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類型定義。

ValueType9+

用于表示允許的數(shù)據(jù)字段類型。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

類型

說明

string

表示string的類型。

image.PixelMap

表示image.PixelMap的類型。

Want

表示Want的類型。

ArrayBuffer

表示ArrayBuffer的類型。

pasteboard.createData9+

createData(mimeType: string, value: ValueType): PasteData

構建一個自定義類型的剪貼板內(nèi)容對象。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

mimeType

string

自定義數(shù)據(jù)的MIME類型。

value

ValueType

自定義數(shù)據(jù)內(nèi)容。

返回值:

類型

說明

PasteData

剪貼板內(nèi)容對象。

示例:

  1. let dataXml = 'Hello World';
  2. let pasteData = pasteboard.createData('app/xml', dataXml);

pasteboard.createRecord9+

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

ValueType

自定義數(shù)據(jù)內(nèi)容。

返回值:

類型

說明

PasteDataRecord

一條新建的自定義數(shù)據(jù)內(nèi)容條目。

示例:

  1. let dataXml = 'Hello World';
  2. let pasteDataRecord = pasteboard.createRecord('app/xml', dataXml);

pasteboard.getSystemPasteboard

getSystemPasteboard(): SystemPasteboard

獲取系統(tǒng)剪貼板對象。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

SystemPasteboard

系統(tǒng)剪貼板對象。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();

ShareOption9+

可粘貼數(shù)據(jù)的范圍類型枚舉。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

名稱

說明

INAPP

0

表示僅允許同應用內(nèi)粘貼。

LOCALDEVICE

1

表示允許在此設備中任何應用內(nèi)粘貼。

CROSSDEVICE

2

表示允許跨設備在任何應用內(nèi)粘貼。

pasteboard.createHtmlData(deprecated)

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)容。

返回值:

類型

說明

PasteData

剪貼板內(nèi)容對象。

示例:

  1. 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>";
  2. let pasteData = pasteboard.createHtmlData(html);

pasteboard.createWantData(deprecated)

createWantData(want: Want): PasteData

構建一個Want剪貼板內(nèi)容對象。

說明

從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用pasteboard.createData替代。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

want

Want

Want內(nèi)容。

返回值:

類型

說明

PasteData

剪貼板內(nèi)容對象。

示例:

  1. let object = {
  2. bundleName: "com.example.aafwk.test",
  3. abilityName: "com.example.aafwk.test.TwoAbility"
  4. };
  5. let pasteData = pasteboard.createWantData(object);

pasteboard.createPlainTextData(deprecated)

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)容。

返回值:

類型

說明

PasteData

剪貼板內(nèi)容對象。

示例:

  1. let pasteData = pasteboard.createPlainTextData('content');

pasteboard.createUriData(deprecated)

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)容。

返回值:

類型

說明

PasteData

剪貼板內(nèi)容對象。

示例:

  1. let pasteData = pasteboard.createUriData('dataability:///com.example.myapplication1/user.txt');

pasteboard.createHtmlTextRecord(deprecated)

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)容。

返回值:

類型

說明

PasteDataRecord

一條新建的HTML內(nèi)容條目。

示例:

  1. 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>";
  2. let record = pasteboard.createHtmlTextRecord(html);

pasteboard.createWantRecord(deprecated)

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

Want內(nèi)容。

返回值:

類型

說明

PasteDataRecord

一條新建的Want內(nèi)容條目。

示例:

  1. let object = {
  2. bundleName: "com.example.aafwk.test",
  3. abilityName: "com.example.aafwk.test.TwoAbility"
  4. };
  5. let record = pasteboard.createWantRecord(object);

pasteboard.createPlainTextRecord(deprecated)

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)容。

返回值:

類型

說明

PasteDataRecord

一條新建的純文本內(nèi)容條目。

示例:

  1. let record = pasteboard.createPlainTextRecord('hello');

pasteboard.createUriRecord(deprecated)

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)容。

返回值:

類型

說明

PasteDataRecord

一條新建的URI內(nèi)容條目。

示例:

  1. let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');

PasteDataProperty7+

定義了剪貼板中所有內(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+

ShareOption

指示剪貼板數(shù)據(jù)可以粘貼到的范圍,如果未設置或設置不正確,則默認值為CROSSDEVICE。

PasteDataRecord7+

對于剪貼板中內(nèi)容記錄的抽象定義,稱之為條目。剪貼板內(nèi)容部分由一個或者多個條目構成,例如一條文本內(nèi)容、一份HTML、一個URI或者一個Want。

屬性

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

名稱

類型

可讀

可寫

說明

htmlText7+

string

HTML內(nèi)容。

want7+

Want

Want內(nèi)容。

mimeType7+

string

數(shù)據(jù)類型。

plainText7+

string

純文本內(nèi)容。

uri7+

string

URI內(nèi)容。

pixelMap9+

image.PixelMap

PixelMap內(nèi)容。

data9+

{[mimeType: string]: ArrayBuffer}

自定義數(shù)據(jù)內(nèi)容。

toPlainText9+

toPlainText(): string

將一個PasteData中的內(nèi)容強制轉(zhuǎn)換為文本內(nèi)容。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

string

純文本內(nèi)容。

示例:

  1. let record = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
  2. let data = record.toPlainText();
  3. console.info(`Succeeded in converting to text. Data: ${data}`);

convertToText(deprecated)

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)容;否則返回錯誤信息。

示例:

  1. let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
  2. record.convertToText((err, data) => {
  3. if (err) {
  4. console.error(`Failed to convert to text. Cause: ${err.message}`);
  5. return;
  6. }
  7. console.info(`Succeeded in converting to text. Data: ${data}`);
  8. });

convertToText(deprecated)

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)容。

示例:

  1. let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
  2. record.convertToText().then((data) => {
  3. console.info(`Succeeded in converting to text. Data: ${data}`);
  4. }).catch((err) => {
  5. console.error(`Failed to convert to text. Cause: ${err.message}`);
  6. });

PasteData

剪貼板內(nèi)容對象。剪貼板內(nèi)容包含一個或者多個內(nèi)容條目(PasteDataRecord)以及屬性描述對象(PasteDataProperty)。

在調(diào)用PasteData的接口前,需要先通過createData()getData()獲取一個PasteData對象。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

getPrimaryText

getPrimaryText(): string

獲取首個條目的純文本內(nèi)容。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

string

純文本內(nèi)容。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let plainText = pasteData.getPrimaryText();

getPrimaryHtml7+

getPrimaryHtml(): string

獲取首個條目的HTML內(nèi)容。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

string

HTML內(nèi)容。

示例:

  1. 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>";
  2. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_HTML, html);
  3. let htmlText = pasteData.getPrimaryHtml();

getPrimaryWant7+

getPrimaryWant(): Want

獲取首個條目的Want對象內(nèi)容。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

Want

Want對象內(nèi)容。

示例:

  1. let object = {
  2. bundleName: "com.example.aafwk.test",
  3. abilityName: "com.example.aafwk.test.TwoAbility"
  4. };
  5. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_WANT, object);
  6. let want = pasteData.getPrimaryWant();

getPrimaryUri7+

getPrimaryUri(): string

獲取首個條目的URI內(nèi)容。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

string

URI內(nèi)容。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
  2. let uri = pasteData.getPrimaryUri();

getPrimaryPixelMap9+

getPrimaryPixelMap(): image.PixelMap

獲取首個條目的PixelMap內(nèi)容。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

image.PixelMap

PixelMap內(nèi)容。

示例:

  1. import image from '@ohos.multimedia.image';
  2. let buffer = new ArrayBuffer(128);
  3. let opt = {
  4. size: { height: 3, width: 5 },
  5. pixelFormat: 3,
  6. editable: true,
  7. alphaType: 1,
  8. scaleMode: 1
  9. };
  10. image.createPixelMap(buffer, opt).then((pixelMap) => {
  11. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_PIXELMAP, pixelMap);
  12. let PixelMap = pasteData.getPrimaryPixelMap();
  13. });

addRecord7+

addRecord(record: PasteDataRecord): void

向當前剪貼板內(nèi)容中添加一條條目,同時也會將條目類型添加到PasteDataProperty的mimeTypes中。入?yún)⒕荒転榭眨駝t添加失敗。

剪貼板內(nèi)容中添加的條目達到數(shù)量上限512后,后續(xù)的添加操作無效。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

record

PasteDataRecord

待添加的條目。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
  2. let textRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  3. 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>";
  4. let htmlRecord = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_HTML, html);
  5. pasteData.addRecord(textRecord);
  6. pasteData.addRecord(htmlRecord);

addRecord9+

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

ValueType

自定義數(shù)據(jù)內(nèi)容。

錯誤碼:

以下錯誤碼的詳細介紹請參見剪貼板錯誤碼

錯誤碼ID

錯誤信息

12900002

The number of record exceeds the maximum limit.

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
  2. let dataXml = 'Hello World';
  3. pasteData.addRecord('app/xml', dataXml);

getMimeTypes7+

getMimeTypes(): Array<string>

獲取剪貼板中PasteDataProperty的mimeTypes列表,當剪貼板內(nèi)容為空時,返回列表為空。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

Array<string>

剪貼板內(nèi)容條目的數(shù)據(jù)類型,非重復的類型列表。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let types = pasteData.getMimeTypes();

getPrimaryMimeType7+

getPrimaryMimeType(): string

獲取剪貼板內(nèi)容中首個條目的數(shù)據(jù)類型。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

string

首個條目的數(shù)據(jù)類型。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let type = pasteData.getPrimaryMimeType();

getProperty7+

getProperty(): PasteDataProperty

獲取剪貼板內(nèi)容的屬性描述對象。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

PasteDataProperty

屬性描述對象。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let property = pasteData.getProperty();

setProperty9+

setProperty(property: PasteDataProperty): void

設置剪貼板內(nèi)容的屬性描述對象,當前僅支持設置shareOption屬性。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

property

PasteDataProperty

屬性描述對象。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_HTML, 'application/xml');
  2. let prop = pasteData.getProperty();
  3. prop.shareOption = pasteboard.ShareOption.INAPP;
  4. pasteData.setProperty(prop);

getRecord9+

getRecord(index: number): PasteDataRecord

獲取剪貼板內(nèi)容中指定下標的條目。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定條目的下標。

返回值:

類型

說明

PasteDataRecord

指定下標的條目。

錯誤碼:

以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。

錯誤碼ID

錯誤信息

12900001

The index is out of the record.

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let record = pasteData.getRecord(0);

getRecordCount7+

getRecordCount(): number

獲取剪貼板內(nèi)容中條目的個數(shù)。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

number

條目的個數(shù)。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let count = pasteData.getRecordCount();

getTag7+

getTag(): string

獲取剪貼板內(nèi)容中用戶自定義的標簽內(nèi)容,如果沒有設置用戶自定義的標簽內(nèi)容將返回空。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

string

返回用戶自定義的標簽內(nèi)容,如果沒有設置用戶自定義的標簽內(nèi)容,將返回空。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let tag = pasteData.getTag();

hasType9+

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。

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let hasType = pasteData.hasType(pasteboard.MIMETYPE_TEXT_PLAIN);

removeRecord9+

removeRecord(index: number): void

移除剪貼板內(nèi)容中指定下標的條目。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定的下標。

錯誤碼:

以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。

錯誤碼ID

錯誤信息

12900001

The index is out of the record.

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. pasteData.removeRecord(0);

replaceRecord9+

replaceRecord(index: number, record: PasteDataRecord): void

替換剪貼板內(nèi)容中指定下標的條目。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定的下標。

record

PasteDataRecord

新條目的內(nèi)容。

錯誤碼:

以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。

錯誤碼ID

錯誤信息

12900001

The index is out of the record.

示例:

  1. let pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, 'hello');
  2. let record = pasteboard.createRecord(pasteboard.MIMETYPE_TEXT_URI, 'dataability:///com.example.myapplication1/user.txt');
  3. pasteData.replaceRecord(0, record);

addHtmlRecord(deprecated)

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)容。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. 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>";
  3. pasteData.addHtmlRecord(html);

addWantRecord(deprecated)

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

Want對象內(nèi)容。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. let object = {
  3. bundleName: "com.example.aafwk.test",
  4. abilityName: "com.example.aafwk.test.TwoAbility"
  5. };
  6. pasteData.addWantRecord(object);

addTextRecord(deprecated)

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)容。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. pasteData.addTextRecord('good');

addUriRecord(deprecated)

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)容。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. pasteData.addUriRecord('dataability:///com.example.myapplication1/user.txt');

getRecordAt(deprecated)

getRecordAt(index: number): PasteDataRecord

獲取剪貼板內(nèi)容中指定下標的條目。

說明

從 API Version 7 開始支持,從 API Version 9 開始廢棄,建議使用getRecord替代。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

index

number

指定條目的下標。

返回值:

類型

說明

PasteDataRecord

指定下標的條目。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. let record = pasteData.getRecordAt(0);

hasMimeType(deprecated)

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。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. let hasType = pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_PLAIN);

removeRecordAt(deprecated)

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。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. let isRemove = pasteData.removeRecordAt(0);

replaceRecordAt(deprecated)

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

PasteDataRecord

替換后的條目。

返回值:

類型

說明

boolean

成功替換返回true,失敗返回false。

示例:

  1. let pasteData = pasteboard.createPlainTextData('hello');
  2. let record = pasteboard.createUriRecord('dataability:///com.example.myapplication1/user.txt');
  3. let isReplace = pasteData.replaceRecordAt(0, record);

SystemPasteboard

系統(tǒng)剪貼板對象。

在調(diào)用SystemPasteboard的接口前,需要先通過getSystemPasteboard獲取系統(tǒng)剪貼板。

  1. let systemPasteboard = pasteboard.getSystemPasteboard();

on('update')7+

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)。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. let listener = () => {
  3. console.info('The system pasteboard has changed.');
  4. };
  5. systemPasteboard.on('update', listener);

off('update')7+

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)。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. let listener = () => {
  3. console.info('The system pasteboard has changed.');
  4. };
  5. systemPasteboard.off('update', listener);

clearData9+

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;否則為錯誤對象。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.clearData((err, data) => {
  3. if (err) {
  4. console.error(`Failed to clear the pasteboard. Cause: ${err.message}`);
  5. return;
  6. }
  7. console.info('Succeeded in clearing the pasteboard.');
  8. });

clearData9+

clearData(): Promise<void>

清空系統(tǒng)剪貼板內(nèi)容,使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

返回值:

類型

說明

Promise<void>

無返回結果的Promise對象。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.clearData().then((data) => {
  3. console.info('Succeeded in clearing the pasteboard.');
  4. }).catch((err) => {
  5. console.error(`Failed to clear the pasteboard. Cause: ${err.message}`);
  6. });

setData9+

setData(data: PasteData, callback: AsyncCallback<void>): void

將數(shù)據(jù)寫入系統(tǒng)剪貼板,使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

data

PasteData

PasteData對象。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當寫入成功,err為undefined,否則為錯誤對象。

錯誤碼:

以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。

錯誤碼ID

錯誤信息

12900003

Another copy or paste is in progress.

12900004

Replication is prohibited.

示例:

  1. let pasteData = pasteboard.createPlainTextData('content');
  2. let systemPasteboard = pasteboard.getSystemPasteboard();
  3. systemPasteboard.setData(pasteData, (err, data) => {
  4. if (err) {
  5. console.error('Failed to set PasteData. Cause: ' + err.message);
  6. return;
  7. }
  8. console.info('Succeeded in setting PasteData.');
  9. });

setData9+

setData(data: PasteData): Promise<void>

將數(shù)據(jù)寫入系統(tǒng)剪貼板,使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.MiscServices.Pasteboard

參數(shù):

參數(shù)名

類型

必填

說明

data

PasteData

PasteData對象。

返回值:

類型

說明

Promise<void>

無返回結果的Promise對象。

錯誤碼:

以下錯誤碼的詳細介紹請參見剪貼板錯誤碼。

錯誤碼ID

錯誤信息

12900003

Another copy or paste is in progress.

12900004

Replication is prohibited.

示例:

  1. let pasteData = pasteboard.createPlainTextData('content');
  2. let systemPasteboard = pasteboard.getSystemPasteboard();
  3. systemPasteboard.setData(pasteData).then((data) => {
  4. console.info('Succeeded in setting PasteData.');
  5. }).catch((err) => {
  6. console.error('Failed to set PasteData. Cause: ' + err.message);
  7. });

getData9+

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.

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.getData((err, pasteData) => {
  3. if (err) {
  4. console.error('Failed to get PasteData. Cause: ' + err.message);
  5. return;
  6. }
  7. let text = pasteData.getPrimaryText();
  8. });

getData9+

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.

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.getData().then((pasteData) => {
  3. let text = pasteData.getPrimaryText();
  4. }).catch((err) => {
  5. console.error('Failed to get PasteData. Cause: ' + err.message);
  6. })

hasData9+

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)容。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.hasData((err, data) => {
  3. if (err) {
  4. console.error(`Failed to check the PasteData. Cause: ${err.message}`);
  5. return;
  6. }
  7. console.info(`Succeeded in checking the PasteData. Data: ${data}`);
  8. });

hasData9+

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)容。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.hasData().then((data) => {
  3. console.info(`Succeeded in checking the PasteData. Data: ${data}`);
  4. }).catch((err) => {
  5. console.error(`Failed to check the PasteData. Cause: ${err.message}`);
  6. });

clear(deprecated)

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;否則為錯誤對象。

示例:

  1. systemPasteboard.clear((err, data) => {
  2. if (err) {
  3. console.error(`Failed to clear the PasteData. Cause: ${err.message}`);
  4. return;
  5. }
  6. console.info('Succeeded in clearing the PasteData.');
  7. });

clear(deprecated)

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對象。

示例:

  1. systemPasteboard.clear().then((data) => {
  2. console.info('Succeeded in clearing the PasteData.');
  3. }).catch((err) => {
  4. console.error(`Failed to clear the PasteData. Cause: ${err.message}`);
  5. });

getPasteData(deprecated)

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ù);否則返回錯誤對象。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.getPasteData((err, pasteData) => {
  3. if (err) {
  4. console.error('Failed to get PasteData. Cause: ' + err.message);
  5. return;
  6. }
  7. let text = pasteData.getPrimaryText();
  8. });

getPasteData(deprecated)

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ù)。

示例:

  1. let systemPasteboard = pasteboard.getSystemPasteboard();
  2. systemPasteboard.getPasteData().then((pasteData) => {
  3. let text = pasteData.getPrimaryText();
  4. }).catch((err) => {
  5. console.error('Failed to get PasteData. Cause: ' + err.message);
  6. })

hasPasteData(deprecated)

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)容。

示例:

  1. systemPasteboard.hasPasteData((err, data) => {
  2. if (err) {
  3. console.error(`Failed to check the PasteData. Cause: ${err.message}`);
  4. return;
  5. }
  6. console.info(`Succeeded in checking the PasteData. Data: ${data}`);
  7. });

hasPasteData(deprecated)

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)容。

示例:

  1. systemPasteboard.hasPasteData().then((data) => {
  2. console.info(`Succeeded in checking the PasteData. Data: ${data}`);
  3. }).catch((err) => {
  4. console.error(`Failed to check the PasteData. Cause: ${err.message}`);
  5. });

setPasteData(deprecated)

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

PasteData對象。

callback

AsyncCallback<void>

回調(diào)函數(shù)。當寫入成功,err為undefined,否則為錯誤對象。

示例:

  1. let pasteData = pasteboard.createPlainTextData('content');
  2. let systemPasteboard = pasteboard.getSystemPasteboard();
  3. systemPasteboard.setPasteData(pasteData, (err, data) => {
  4. if (err) {
  5. console.error('Failed to set PasteData. Cause: ' + err.message);
  6. return;
  7. }
  8. console.info('Succeeded in setting PasteData.');
  9. });

setPasteData(deprecated)

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

PasteData對象。

返回值:

類型

說明

Promise<void>

無返回結果的Promise對象。

示例:

  1. let pasteData = pasteboard.createPlainTextData('content');
  2. let systemPasteboard = pasteboard.getSystemPasteboard();
  3. systemPasteboard.setPasteData(pasteData).then((data) => {
  4. console.info('Succeeded in setting PasteData.');
  5. }).catch((err) => {
  6. console.error('Failed to set PasteData. Cause: ' + err.message);
  7. });
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號