# private_window **Repository Path**: scenario-samples/private_window ## Basic Information - **Project Name**: private_window - **Description**: 【鸿蒙 Harmony Next 示例 代码】 这是一个flutter插件应用,example为测试private_window隐私窗口插件的目录,ohos目录为插件模块内容 PrivateWindow.ets和PrivateWindowPlugin.ets为插件代码 功能:进行隐私窗口拦截处理,防止窗口被截屏 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-13 - **Last Updated**: 2025-05-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # private_window隐私窗口插件使用介绍 ## 介绍 * 这是一个flutter插件应用,example为测试private_window隐私窗口插件的目录,ohos目录为插件模块内容 * PrivateWindow.ets和PrivateWindowPlugin.ets为插件代码 * 功能:进行隐私窗口拦截处理,防止窗口被截屏 ## 效果预览 ## 约束与限制 1.本示例仅支持标准系统上运行,支持设备:华为手机。 2.HarmonyOS系统:HarmonyOS NEXT Release及以上。 3.DevEco Studio版本:DevEco Studio 5.0.1 Release及以上。 4.HarmonyOS SDK版本:HarmonyOS 5.0.1 Release SDK及以上。 ## 使用说明 执行一下步骤前,请先进行flutter所需的环境配置,可参考[flutter](https://gitee.com/openharmony-sig/flutter_flutter) 执行步骤如下: 1. 进入到example目录,进行flutter pub get命令将下载所需要的依赖 2. 使用flutter build hap 命令构建所需要的har包,呈现结果如下:在private_window/example/ohos/har目录下存在 flutter.har和private_window.har的包 3. 使用flutter run -d xxx设备运行flutter项目 4. 进入flutter主页,进行手动截屏操作可发现会弹出无法截屏的提示。 ## 权限说明 ohos项目,需要配置如下权限: ```json {"name": "ohos.permission.PRIVACY_WINDOW"} ``` example下的项目,需要配置网络权限: ```json {"name" : "ohos.permission.INTERNET"} ``` ## 实现思路 1. 隐私窗口的实现主要依赖于window对象的setWindowPrivacyMode来设置窗口的隐私模式: ```typescript export default class PrivateWindow { setPrivateWindow(): void { let context = getContext(this) as common.UIAbilityContext; // 1.获取应用主窗口。 let windowClass: window.Window | null = null; //用于获取最后一个打开的窗口。 window.getLastWindow(context, (err, data) => { windowClass = data; // 2.设置为隐私模式的窗口 let isPrivacyMode: boolean = true; try { windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { hilog.error(domainId, tag, 'Failed to set the window to privacy mode. Cause:' + JSON.stringify(err)); return; } hilog.info(domainId, tag, 'Succeeded in setting the window to privacy mode.'); }); } catch (exception) { hilog.error(domainId, tag, 'Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception)); } }); } } ``` 2. 关闭窗口隐私模式 ```typescript export default class PrivateWindow { unSetPrivateWindow(): void { let context = getContext(this) as common.UIAbilityContext; // 1.获取应用主窗口。 let windowClass: window.Window | null = null; window.getLastWindow(context, (err, data) => { windowClass = data; hilog.info(domainId, tag, 'Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); // 2.取消隐私模式的窗口 let isPrivacyMode: boolean = false; try { windowClass.setWindowPrivacyMode(isPrivacyMode, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { hilog.error(domainId, tag, 'Failed to set the window to privacy mode. Cause:' + JSON.stringify(err)); return; } hilog.info(domainId, tag, 'Succeeded in setting the window to privacy mode.'); }); } catch (exception) { hilog.error(domainId, tag, 'Failed to set the window to privacy mode. Cause:' + JSON.stringify(exception)); } }); } } ``` 3. 然后结合flutter plugin,在程序运行时开启窗口的隐私模式即可 ## 工程目录 ``` private_window |---lib // Flutter相关 | |---private_window.dart | |---private_window_method_channel.dart | |---private_window_platform_interface.dart ohos/entry/src/main/ets |---componentes | |---PrivateWindow.ets | |---PrivateWindowPlugin.ets example/lib |---main.dart example/ohos/entry/src/main/ets/ |---entryability | |---EntryAbility.ets |---pages | |---Index.ets |---plugins | |---GeneratedPluginRegistrant.ets example/ohos/entry/src/ohosTest/ets |---test | |---Ability.test.ets | |---List.test.ets |---testability | |---pages | | |---Index.ets | |---TestAbility.ets |---testrunner | |---OpenHarmonyTestRunner.ts ``` ## 模块依赖 暂无 ## 参考文档 [1] [flutter参考文档](https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin-platforms) [2] [窗口](https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5) ## ChangeLog | 修改内容 | 时间 | |------|------------| | 完成 | 2025.10.10| ## 一份简单的问卷反馈 亲爱的Harmony Next开发者,您好!
为了协助您高效开发,提高鸿蒙场景化示例的质量,希望您在浏览或使用后抽空填写一份简单的问卷,我们将会收集您的宝贵意见进行优化:heart: [:arrow_right: **点击此处填写问卷** ](https://wj.qq.com/s2/19042938/95ab/)