先使用getLastWindow获取window示例,(也可以在EntryAbility的onWindowStageCreate里存储windowStage对象,在需要使用的地方直接通过AppStorage获取windowStage对象)再使用setWindowBrightness来设置屏幕亮度,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5\#setwindowbrightness9import window from '@ohos.window'; @Entry @Component struct WindowStagePage { aboutToAppear(): void { window.getLastWindow(getContext(),(err,data)=>{ const errCode=err.code; if(errCode){ console.log('window:',JSON.stringify(err)) return; } data.setWindowBrightness(1.0); }) } aboutToDisappear(): void { window.getLastWindow(getContext(),(err,data)=>{ const errCode=err.code; if(errCode){ console.log('window:',JSON.stringify(err)) return; } data.setWindowBrightness(-1.0); }) } build() { Column({space:10}) { Button("变亮") .onClick(() => { window.getLastWindow(getContext(),(err,data)=>{ const errCode=err.code; if(errCode){ console.log('window:',JSON.stringify(err)) return; } data.setWindowBrightness(1.0); }) }) Button("变暗") .onClick(() => { window.getLastWindow(getContext(),(err,data)=>{ const errCode=err.code; if(errCode){ console.log('window:',JSON.stringify(err)) return; } data.setWindowBrightness(0.0); }) }) Button("恢复") .onClick(() => { window.getLastWindow(getContext(),(err,data)=>{ const errCode=err.code; if(errCode){ console.log('window:',JSON.stringify(err)) return; } data.setWindowBrightness(-1.0); }) }) } .width('100%') } }
先使用getLastWindow获取window示例,(也可以在EntryAbility的onWindowStageCreate里存储windowStage对象,在需要使用的地方直接通过AppStorage获取windowStage对象)
再使用setWindowBrightness来设置屏幕亮度,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5\#setwindowbrightness9