PasteboardUtil, Clipboard Tools

PasteboardUtil, Clipboard Tools

Publish Date: Jun 29
0 0

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


requestPermissions request clipboard permissions
PasteboardUtil.requestPermissions().then((result) => {
  ToastUtil.showToast(`检测并申请授权,允许应用读取剪贴板:${result}`);
});
Enter fullscreen mode Exit fullscreen mode
getSystemPasteboard Gets the system clipboard object
 let systemPasteboard = PasteboardUtil.getSystemPasteboard();
Enter fullscreen mode Exit fullscreen mode
hasData determines whether there is any content in the system clipboard
let hasData = PasteboardUtil.hasDataSync();
ToastUtil.showToast(`系统剪贴板中是否有内容:${hasData}`);
Enter fullscreen mode Exit fullscreen mode
setData Writes data to the system clipboard
let text = "一款高效的OpenHarmony/HarmonyOS工具包。"
PasteboardUtil.setDataSync(pasteboard.MIMETYPE_TEXT_PLAIN, text);
Enter fullscreen mode Exit fullscreen mode
getData Reads the contents of the system clipboard
let text = PasteboardUtil.getDataSync().getPrimaryText();
ToastUtil.showToast(`剪切板内容为:${text}`);
Enter fullscreen mode Exit fullscreen mode
setDataText Write plain text data to the system clipboard
let text = "harmony-utils 一款高效的OpenHarmony/HarmonyOS工具包,封装了常用工具类,提供一系列简单易用的方法。"
PasteboardUtil.setDataText(text).then(() => {
  ToastUtil.showToast("设置成功!");
})
Enter fullscreen mode Exit fullscreen mode
getDataText Read plain text content of the system clipboard
let str = await PasteboardUtil.getDataText();
ToastUtil.showToast(`剪切板内容为:${str}`);
Enter fullscreen mode Exit fullscreen mode
setDataHtml Write HTML data to the system clipboard
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>";
PasteboardUtil.setDataHtml(html).then(() => {
  ToastUtil.showToast("设置成功");
})
Enter fullscreen mode Exit fullscreen mode
getDataHtml Read the HTML content of the system clipboard
 let html = await PasteboardUtil.getDataHtml();
 ToastUtil.showToast(`剪切板内容为:${html}`);
Enter fullscreen mode Exit fullscreen mode
setDataUri Write URI data to the system clipboard
let dataUri = 'dataability:///com.example.myapplication1/user.txt';
PasteboardUtil.setDataUri(dataUri).then(() => {
  ToastUtil.showToast("设置成功");
})
Enter fullscreen mode Exit fullscreen mode
getDataUri Reads the URI content of the system clipboard URI
let uri = PasteboardUtil.getDataUriSync();
ToastUtil.showToast(`剪切板内容为:${uri}`);
Enter fullscreen mode Exit fullscreen mode
setDataWant Write Want data to the system clipboard
let want: Want = {
  deviceId: 'device_666666',
  bundleName: 'com.example.myapplication',
  abilityName: 'FuncAbility',
  moduleName: 'entry'
};
PasteboardUtil.setDataWantSync(want);
Enter fullscreen mode Exit fullscreen mode
getDataWant Reads the Want content of the system clipboard
let want = PasteboardUtil.getDataWantSync();
if (want) {
  let jsonStr = JSON.stringify(want, null, 2);
  Utils.showSheetText(jsonStr);
}else {
  ToastUtil.showToast("剪切板内容为空,请先设置内容");
}
Enter fullscreen mode Exit fullscreen mode
setDataPixelMap Write PixelMap data to the system clipboard
let pixelMap = await ImageUtil.getPixelMapFromMedia($r("app.media.test_as4"))
PasteboardUtil.setDataPixelMap(pixelMap).then(() => {
  ToastUtil.showToast("设置成功");
});
Enter fullscreen mode Exit fullscreen mode
getDataPixelMap Read the content of the system clipboard PixelMap
let pixelMap = PasteboardUtil.getDataPixelMapSync();
if (pixelMap) {
  Utils.showSheetImg(pixelMap);
} else {
  ToastUtil.showToast("剪切板内容为空,请先设置内容");
}
Enter fullscreen mode Exit fullscreen mode
getDataStr Read strings in the system clipboard
let str = PasteboardUtil.getDataStrSync();
ToastUtil.showToast(`剪切板内容为:${str}`);
Enter fullscreen mode Exit fullscreen mode
getDataEasy Read content in the system clipboard (plain text content, HTML content, URI content, Want content, PixelMap content)
let data = PasteboardUtil.getDataEasy();
if (data && typeof data === 'string') {
  Utils.showSheetText(data);
} else if (data && typeof data === 'object') {
  if (typeof (data as PixelMap).getImageInfo === 'function') {
    Utils.showSheetImg(data as PixelMap);
  } else {
    Utils.showSheetText(JSON.stringify(data, null, 2));
  }
} else {
  ToastUtil.showToast("剪切板内容为空,请先设置内容");
}
Enter fullscreen mode Exit fullscreen mode
clearData Clear system clipboard content
PasteboardUtil.clearDataSync();
ToastUtil.showToast(`清空成功!`);
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment