1、一些页面需要实现防录屏功能,首先需要在module.json5中申请隐私窗口权限
"requestPermissions": [
{
"name" : 'ohos.permission.PRIVACY_WINDOW'
}
],
2、在EntryAbility文件中
let windowClass: window.Window = windowStage.getMainWindowSync();
// 获取应用主窗口
AppStorage.setOrCreate("windowClass",windowClass);
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
3、Index文件中
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct Index {
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%')
}
}