【HarmonyOS NEXT】实现防截屏功能
【需求】
- 登录不允许截屏
- 验证码页不允许截屏
- 首页允许截屏
【API】
方式一:
- 当app只有一个窗口时,可以使用getLastWindow
window.getLastWindow(context).then((lastWindow)=>{ lastWindow.setWindowPrivacyMode(flag) })
方式二:
当app存在多个窗口时
- 在EntryAbility中保存windowStage
AppStorage.setOrCreate('windowStage', windowStage);
- 在登录页调
windowStage.getMainWindowSync().setWindowPrivacyMode(flag)
【代码】
import { router, window } from '@kit.ArkUI'
@Entry
@Component
struct LoginPage {
onPageShow(): void {
this.setWindowPrivacyModeInPage(true)
}
onPageHide(): void {
this.setWindowPrivacyModeInPage(false)
}
setWindowPrivacyModeInPage(flag: boolean) {
const windowStage = AppStorage.get('windowStage') as window.WindowStage
windowStage.getMainWindowSync().setWindowPrivacyMode(flag)
}
build() {
Column({ space: 20 }) {
Text('登录页').fontSize(30).padding({ top: 60 })
Button('去验证码页').onClick(() => {
router.replaceUrl({
url: 'pages/Case/AntiScreenshot/CodePage'
})
})
Button('去首页').onClick(() => {
router.replaceUrl({
url: 'pages/Case/AntiScreenshot/MainPage'
})
})
}
.height('100%')
.width('100%')
}
}
【效果图】
因为做的是防截屏功能,所以没有效果图~
原文地址:https://blog.csdn.net/xliang111/article/details/142584218
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!