如果仅需要在部分页面实现诸如设置隐私模式,窗口旋转等功能,需要先获取到窗口实例,再通过窗口实例去设置隐私模式或窗口旋转等功能。以官网如何实现防截屏功能为例,通过方式二在PageOne和PageTwo设置隐私模式,在频繁切换页面时,有时隐私模式会失效,导致该页面可以截屏。
问题代码如下:
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
class WindowUtils {
static setWindowPrivacyModeInPage(context: common.UIAbilityContext, isFlag: boolean) {
window.getLastWindow(context).then((lastWindow) => {
// 通过窗口实例调用setWindowPrivacyMode设置隐私模式
lastWindow.setWindowPrivacyMode(isFlag, (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.');
});
});
}
}
@Entry
@Component
struct Index {
@State message: string = 'hello world';
@Provide('NavPathStack') pageStack: NavPathStack = new NavPathStack();
context = this.getUIContext();
@Builder
PagesMap(name: string) {
if (name === 'Index') {
Index();
} else if (name === 'PageOne') {
PageOne();
} else if (name === 'PageTwo') {
PageTwo();
}
}
build() {
Navigation(this.pageStack) {
Column() {
Button('到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageStack.replacePathByName('PageOne', null,
false);
});
};
}
.navDestination(this.PagesMap);
}
}
@Component
struct PageOne {
@Consume('NavPathStack') pageStack: NavPathStack;
build() {
NavDestination() {
Column() {
Text('PageOne');
Button('到PageTwo', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageStack.replacePathByName('PageTwo', null,
false);
});
};
}
.title('pageOne')
.onShown(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, true);
})
.onHidden(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, false);
});
}
}
@Component
struct PageTwo {
@Consume('NavPathStack') pageStack: NavPathStack;
build() {
NavDestination() {
Column() {
Text('PageTwo');
Button('到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageStack.replacePathByName('PageOne', null,
false);
});
};
}
.title('pageTwo')
.onShown(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, true);
})
.onHidden(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, false);
});
}
} 正常情况下,当反复切换页面时,隐私模式应该由开启到关闭再到开启往复循环。

但日志中mode出现了连续为1和连续为0情况,说明隐私模式不是从开启到关闭进行循环。
可以通过windowStage.getMainWindowSync的同步方式获取窗口实例。再去设置隐私模式。
onWindowStageCreate(windowStage: window.WindowStage): void {
AppStorage.setOrCreate('windowStage', windowStage); import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
class WindowUtils {
static setWindowPrivacyModeInPage(context: common.UIAbilityContext, isFlag: boolean) {
console.info(`${context}`);
let windowStage = AppStorage.get('windowStage') as window.WindowStage;
let windowClass = windowStage.getMainWindowSync();
windowClass.setWindowPrivacyMode(isFlag, (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.');
});
}
}
@Entry
@Component
struct Index {
@Provide('NavPathStack') pageStack: NavPathStack = new NavPathStack();
@Builder
PagesMap(name: string) {
if (name === 'Index') {
Index();
} else if (name === 'PageOne') {
PageOne();
} else if (name === 'PageTwo') {
PageTwo();
}
}
build() {
Navigation(this.pageStack) {
Column() {
Button('到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageStack.replacePathByName('PageOne', null,
false);
});
};
}
.navDestination(this.PagesMap);
}
}
@Component
struct PageOne {
@Consume('NavPathStack') pageStack: NavPathStack;
build() {
NavDestination() {
Column() {
Text('PageOne');
Button('到PageTwo', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageStack.replacePathByName('PageTwo', null,
false);
});
};
}
.title('pageOne')
.onShown(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, true);
})
.onHidden(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, false);
});
}
}
@Component
struct PageTwo {
@Consume('NavPathStack') pageStack: NavPathStack;
build() {
NavDestination() {
Column() {
Text('PageTwo');
Button('到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.width('80%')
.height(40)
.margin(20)
.onClick(() => {
this.pageStack.replacePathByName('PageOne', null,
false);
});
};
}
.title('pageTwo')
.onShown(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, true);
})
.onHidden(() => {
WindowUtils.setWindowPrivacyModeInPage(this.getUIContext().getHostContext() as common.UIAbilityContext, false);
});
}
}