Screenshot Prevention, Flutter Popups, Clipboard Functions, Cross-File Style Reuse, Reading Page Turning

[Daily HarmonyOS Next Knowledge] Screenshot Prevention, Flutter Popups, Clipboard Functions, Cross-File Style Reuse, Reading Page Turning 1. How to prevent screenshot/screen recording for all pages in a HarmonyOS app? Some pages require screenshot prevention. First, apply for the privacy window permission in module.json5. Then, obtain the current window object and set privacy mode in onPageShow(). Cancel the privacy mode in the onPageHide() lifecycle when exiting the page. Reference document: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5 The window module provides basic capabilities for managing windows, including creation, destruction, attribute settings, and scheduling between windows. Key functions related to windows: Window: The current window instance, the basic unit managed by the window manager. WindowStage: The window manager, responsible for managing each basic window unit. Reference code: // In module.json5 { "name": "ohos.permission.PRIVACY_WINDOW" } // In EAbility file onWindowStageCreate(windowStage: window.WindowStage): void { let windowClass: window.Window = windowStage.getMainWindowSync(); // Get the main application window AppStorage.setOrCreate("windowClass", windowClass); } // In Index file import { window } from '@kit.ArkUI'; import { BusinessError } from '@ohos.base'; @Entry @Component struct Index43 { private windowStage = AppStorage.get("windowStage") as window.WindowStage; private windowClass = AppStorage.get("windowClass") as window.Window; @State message: string = 'Hello World'; build() { Row() { Column() { Text('禁止截屏') .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let isPrivacyMode: boolean = true; try { this.windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err)); return; } console.info('Succeeded in setting the window to privacy mode.'); }); } catch (exception) { console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception)); } }); Text('允许截屏') .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let isPrivacyMode: boolean = false; try { this.windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err)); return; } console.info('Succeeded in setting the window to privacy mode.'); }); } catch (exception) { console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception)); } }); } .width('100%'); } .height('100%'); } } 2. When returning to a Flutter transparent popup (dialog) from a full-screen page in HarmonyOS, the popup's entry animation repeats. When a Flutter transparent popup opens a full-screen page (native or Flutter), the popup executes an exit animation. When returning to the popup, it executes an entry animation again. Try removing the animation effect after the animation ends in the Dialog page: @State isInit: boolean = false; .onHidden(() => { this.isInit = true }) .transition(this.isInit ? null : this.effect) 3. How to use the clipboard function in HarmonyOS? Requirement: Long-press a string of text to directly add its content to the clipboard without popping up a text selection dialog, ensuring successful copying. Clipboard usage demo: import pasteboard from '@ohos.pasteboard'; import promptAction from '@ohos.promptAction'; @Entry @Component export struct CopyText { private textContent: string = "复制我"; build() { Column() { Text(this.textContent) .fontSize($r("sys.float.ohos_id_text_size_body3")) .borderRadius(9) .borderWidth(1) .padding({ left: 8, right: 8 }) .fontColor($r('sys.color.ohos_id_color_text_primary')) .fontWeight(FontWeight.Medium) .opacity($r("sys.float.ohos_id_alpha_content_secondary")) .onClick(() => copyText(this.textContent)); } } } function copyText(text: string) { const pasteboardData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text); const systemPasteboard = pasteboard.getSystemPasteboard(); systemPasteboard.setData(pasteboardData); // Put data into the clipboard systemPasteboard.getData().then((data) => {

Jun 25, 2025 - 17:50
 0
Screenshot Prevention, Flutter Popups, Clipboard Functions, Cross-File Style Reuse, Reading Page Turning

[Daily HarmonyOS Next Knowledge] Screenshot Prevention, Flutter Popups, Clipboard Functions, Cross-File Style Reuse, Reading Page Turning

1. How to prevent screenshot/screen recording for all pages in a HarmonyOS app?

Some pages require screenshot prevention. First, apply for the privacy window permission in module.json5. Then, obtain the current window object and set privacy mode in onPageShow(). Cancel the privacy mode in the onPageHide() lifecycle when exiting the page.

Reference document: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5

The window module provides basic capabilities for managing windows, including creation, destruction, attribute settings, and scheduling between windows.

Key functions related to windows:

  • Window: The current window instance, the basic unit managed by the window manager.
  • WindowStage: The window manager, responsible for managing each basic window unit.

Reference code:

// In module.json5
{
  "name": "ohos.permission.PRIVACY_WINDOW"
}
// In EAbility file
onWindowStageCreate(windowStage: window.WindowStage): void {
  let windowClass: window.Window = windowStage.getMainWindowSync();
  // Get the main application window
  AppStorage.setOrCreate("windowClass", windowClass);
}
// In Index file
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index43 {
  private windowStage = AppStorage.get("windowStage") as window.WindowStage;
  private windowClass = AppStorage.get("windowClass") as window.Window;
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text('禁止截屏')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let isPrivacyMode: boolean = true;
            try {
              this.windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => {
                const errCode: number = err.code;
                if (errCode) {
                  console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err));
                  return;
                }
                console.info('Succeeded in setting the window to privacy mode.');
              });
            } catch (exception) {
              console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception));
            }
          });
        Text('允许截屏')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let isPrivacyMode: boolean = false;
            try {
              this.windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => {
                const errCode: number = err.code;
                if (errCode) {
                  console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(err));
                  return;
                }
                console.info('Succeeded in setting the window to privacy mode.');
              });
            } catch (exception) {
              console.error('Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception));
            }
          });
      }
      .width('100%');
    }
    .height('100%');
  }
}

2. When returning to a Flutter transparent popup (dialog) from a full-screen page in HarmonyOS, the popup's entry animation repeats.

When a Flutter transparent popup opens a full-screen page (native or Flutter), the popup executes an exit animation. When returning to the popup, it executes an entry animation again.

Try removing the animation effect after the animation ends in the Dialog page:

@State isInit: boolean = false;
.onHidden(() => {
  this.isInit = true
})
.transition(this.isInit ? null : this.effect)

3. How to use the clipboard function in HarmonyOS?

Requirement: Long-press a string of text to directly add its content to the clipboard without popping up a text selection dialog, ensuring successful copying.

Clipboard usage demo:

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

@Entry
@Component
export struct CopyText {
  private textContent: string = "复制我";

  build() {
    Column() {
      Text(this.textContent)
        .fontSize($r("sys.float.ohos_id_text_size_body3"))
        .borderRadius(9)
        .borderWidth(1)
        .padding({ left: 8, right: 8 })
        .fontColor($r('sys.color.ohos_id_color_text_primary'))
        .fontWeight(FontWeight.Medium)
        .opacity($r("sys.float.ohos_id_alpha_content_secondary"))
        .onClick(() => copyText(this.textContent));
    }
  }
}

function copyText(text: string) {
  const pasteboardData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text);
  const systemPasteboard = pasteboard.getSystemPasteboard();
  systemPasteboard.setData(pasteboardData); // Put data into the clipboard
  systemPasteboard.getData().then((data) => {
    if (data) {
      promptAction.showToast({ message: "复制成功" });
    } else {
      promptAction.showToast({ message: "复制失败" });
    }
  });
}

4. How to reuse styles across files in HarmonyOS?

  1. @styles or @Extend do not currently support export, and these decorators will not evolve further.
  2. It is recommended to use the new style reuse method: dynamically set components through the attributeModifier attribute. Inherit the Modifier of the base component via a custom class and set reusable attributes in the class, which has no export restrictions.

Reference link: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-attribute-modifier-V5

5. Does HarmonyOS have official demos for implementing reading page-turning functionality?