可以参考以下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() { } }
可以参考以下demo来实现动态禁止截屏录屏。
首先需要在module.json5文件中声明需要使用的 ohos.permission.PRIVACY\_WINDOW 权限
1:创建windowUtils文件
2:EntryAbility
3:Index文件中