SDK通过设置主窗口为隐私模式实现防截屏,启动应用时全局可防截屏。进入登录页面后,页面开启隐私模式,离开登录页面后取消隐私模式。离开登录页面时取消隐私模式的操作导致SDK设置的主窗口隐私模式被取消,因此全局防截屏失效。
在进入登录页面时通过getWindowProperties().isPrivacyMode获取当前窗口的隐私模式状态并保存,在离开登录页面时恢复窗口隐私模式状态。示例代码如下:
"requestPermissions": [
{
"name": "ohos.permission.PRIVACY_WINDOW",
"reason": "$string:app_name"
}
], import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; const DOMAIN = 0x0000; export default class EntryAbility extends UIAbility { onCreate(): void { try { this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } catch (err) { hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err)); } hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); } onDestroy(): void { hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created,set main page for this ability hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); // Get the main window windowStage.getMainWindow((err: BusinessError, data) => { let errCode: number = err.code; if (errCode) { console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); return; } let windowClass: window.Window = data; console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); // Set the window privacy mode let isPrivacyMode: boolean = true; try { 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)); } }); windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; } hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); } };
@Entry @Component struct Index { private pagePath: NavPathStack = new NavPathStack(); build() { Navigation(this.pagePath) { Column() { Button('PrivacyWindow') .onClick(() => { this.pagePath.pushPathByName('PageOne', false); }); }; } .mode(NavigationMode.Stack) .hideTitleBar(true) .hideToolBar(true); } }
import { common } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; // 跳转页面入口函数 @Builder export function PageOneBuilder() { PageOne(); } // 设置页面隐私窗口状态类 class windowUtils { static setWindowPrivacyModeInPage(context: common.UIAbilityContext, isFlag: boolean) { window.getLastWindow(context).then((lastWindow) => { lastWindow.setWindowPrivacyMode(isFlag, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { console.error(`Failed to set the window to privacy mode. Cause: ${errCode}`); return; } console.info('Succeeded in setting the window to privacy mode.'); }); }); } } @Entry @Component struct PageOne { pathStack: NavPathStack = new NavPathStack(); context = this.getUIContext().getHostContext(); @State windowState: boolean = false; @Builder pageOne() { Column({ space: 10 }) { Text('This is PageOne'); Button('Pop上一页') .onClick(() => { this.pathStack.pop({ a: 'aaa' }); }); }; } build() { NavDestination() { this.pageOne(); } .title('PageOne') .onReady((context: NavDestinationContext) => { this.pathStack = context.pathStack; }) .onWillShow(() => { window.getLastWindow(this.getUIContext().getHostContext()).then(win => { console.info(`win.getWindowProperties().isPrivacyMode:${win.getWindowProperties().isPrivacyMode}`); // 获取并保存窗口隐私模式状态 this.windowState = win.getWindowProperties().isPrivacyMode; }); windowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as Context as common.UIAbilityContext, true); }) .onWillHide(() => { // 恢复窗口隐私模式状态 windowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as Context as common.UIAbilityContext, this.windowState); }); } }