HarmonyOS 禁止录屏截屏?

需要在有密码的页面禁止录屏截屏。有没有API.

阅读 489
1 个回答

可以参考以下demo来实现动态禁止截屏录屏。

首先需要在module.json5文件中声明需要使用的 ohos.permission.PRIVACY\_WINDOW 权限

1:创建windowUtils文件

//windowUtils 文件
import window from '@ohos.window';
import common from '@ohos.app.ability.common';

export class windowUtils {
  static setWindowPrivacyModeInPage(context: common.UIAbilityContext,isFlag: boolean) {
    window.getLastWindow(context).then((lastWindow)=>{
      lastWindow.setWindowPrivacyMode(isFlag);
    })
  }
}

2:EntryAbility

//EAbility文件中

onWindowStageCreate(windowStage: window.WindowStage): void {
  let windowClass: window.Window = windowStage.getMainWindowSync();
  // 获取应用主窗口

  AppStorage.setOrCreate("windowClass",windowClass);
}

3:Index文件中

import { window } from '@kit.ArkUI';
import { windowUtils } from '../component/windowUtils';
import { common } from '@kit.AbilityKit';

@Entry
@Component
struct Index {

  private windowClass = AppStorage.get("windowClass") as window.Window
  @State message: string = 'Hello World';

  aboutToAppear(): void {
    try {
      //开启固定态输入法窗口软键盘高度变化的监听
      this.windowClass.on('keyboardHeightChange', (data) => {
        console.info('Succeeded in enabling the listener for keyboard height changes. Data: ' + JSON.stringify(data));

        //键盘弹出,截止截屏
        if(data>0){
          windowUtils.setWindowPrivacyModeInPage(getContext(this) as common.UIAbilityContext, true);
        }
        else {
          windowUtils.setWindowPrivacyModeInPage(getContext(this) as common.UIAbilityContext, false);
        }

      });
    } catch (exception) {
      console.error('Failed to enable the listener for keyboard height changes. Cause: ' + JSON.stringify(exception));
    }

  }
  build() {
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进