HarmonyOS 鸿蒙Next 登录界面防截屏/录屏有没有参考代码
HarmonyOS 鸿蒙Next 登录界面防截屏/录屏有没有参考代码
登录界面防截屏/录屏有没有参考代码
2 回复
参考代码如下: 1.在module.json5添加ohos.permission.PRIVACY_WINDOW权限
2.在EAbility文件中添加下面的代码:
onWindowStageCreate(windowStage: window.WindowStage): void {
let windowClass: window.Window = windowStage.getMainWindowSync();
// 获取应用主窗口
AppStorage.setOrCreate("windowClass", windowClass);
}
3.在所在页面添加代码:
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
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%')
}
}
更多关于HarmonyOS 鸿蒙Next 登录界面防截屏/录屏有没有参考代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对HarmonyOS 鸿蒙Next登录界面防截屏/录屏的需求,可以参考以下代码实现:
可以使用setWindowPrivacyMode
方法设置窗口为隐私模式,从而禁止截屏或录屏。以下是一个示例代码:
import { window } from '@kit.ArkUI';
function setPrivacyMode(flag: boolean) {
window.getLastWindow().then((lastWindow) => {
lastWindow.setWindowPrivacyMode(flag, (err) => {
if (err) {
console.error('Failed to set the window to privacy mode.', err);
} else {
console.info('Succeeded in setting the window to privacy mode.');
}
});
});
}
// 在登录页面显示时调用,设置隐私模式为true
function onLoginPageShow() {
setPrivacyMode(true);
}
// 在登录页面隐藏时调用,恢复隐私模式为false(可选)
function onLoginPageHide() {
setPrivacyMode(false);
}
在上述代码中,setPrivacyMode
函数用于设置当前窗口的隐私模式。当flag
为true
时,窗口内容将无法被截屏或录屏。
请确保在调用这些方法前已经获得了必要的权限,即ohos.permission.PRIVACY_WINDOW
。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。