harmony 鸿蒙保持屏幕常亮

  • 2023-06-24
  • 浏览 (1188)

保持屏幕常亮

场景说明

在使用开发板进行应用开发调试时,不操作屏幕一段时间后,屏幕会熄屏变黑,再操作时需要再次点亮屏幕,耗费时间不利于调试。本例将介绍如何通过窗口管理相关接口实现屏幕常亮。

效果呈现

本示例最终效果如下(在开发板中可验证实际常亮效果):

screenon

运行环境

本例基于以下环境开发,开发者也可以基于其他适配的版本进行开发:

  • IDE:DevEco Studio 3.1 Release
  • SDK:Ohos_sdk_public 3.2.12.2 (API Version 9 Release)

实现思路

通过Window的setWindowKeepScreenOn方法,在需要时控制屏幕是否常亮,并通过Window的getWindowProperties().isKeepScreenOn查看窗口属性中屏幕常亮的状态。

开发步骤

  1. 构建简易界面,用于显示当前屏幕常亮的状态,并绘制按钮用于设置常亮和取消常亮。
   build() {
   	Row() {
   	  Column() {
   		Text(this.message + this.isScreenOn)
   		  ...
   
   		Button('设置常亮')
   		  ...
   		  .onClick(()=>{
   			this.keepScreenOn(true)
   		  })
   
   		Button('取消常亮')
   		  ...
   		  .onClick(()=>{
   			this.keepScreenOn(false)
   		  })
   	  }
   	  .width('100%')
   	}
   	.height('100%')
   }
  1. 获取应用上下文并通过其获取到当前窗口实例,通过窗口实例的setWindowKeepScreenOn方法控制屏幕是否常亮,并通过getWindowProperties获取窗口属性,从而查看属性中屏幕常亮状态isKeepScreenOn的取值。
   import common from '@ohos.app.ability.common';
   import window from '@ohos.window';
   
   private context: common.BaseContext = getContext(this) as common.BaseContext
   
   async keepScreenOn(status) {
   	let windowClass = await window.getLastWindow(this.context) //获取窗口实例
   	await windowClass.setWindowKeepScreenOn(status) //设置窗口常亮或取消
   	this.isScreenOn = await windowClass.getWindowProperties().isKeepScreenOn//查看屏幕常亮状态
   }

完整代码

通过上述步骤可以完成整个示例的开发,完整代码如下:

import common from '@ohos.app.ability.common';
import window from '@ohos.window';

@Entry
@Component
struct KeepScreenOn {
  @State message: string = '常亮状态 : '
  @State isScreenOn: boolean = false
  private context: common.BaseContext = getContext(this) as common.BaseContext

  async keepScreenOn(status) {
    let windowClass = await window.getLastWindow(this.context) //获取窗口实例
    await windowClass.setWindowKeepScreenOn(status) //设置窗口常亮或取消
    this.isScreenOn = await windowClass.getWindowProperties().isKeepScreenOn//查看屏幕常亮状态
  }

  build() {
    Row() {
      Column() {
        Text(this.message + this.isScreenOn)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)

        Button('设置常亮')
          .fontSize(16)
          .height(50)
          .width(100)
          .borderRadius(10)
          .margin({top:20})
          .backgroundColor('#A4AE77')
          .onClick(()=>{
            this.keepScreenOn(true)
          })

        Button('取消常亮')
          .fontSize(16)
          .height(50)
          .width(100)
          .borderRadius(10)
          .margin({top:20})
          .backgroundColor('#A4AE77')
          .onClick(()=>{
            this.keepScreenOn(false)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙案例介绍

harmony 鸿蒙应用质量提升案例-应用Crash闪退问题案例分析

harmony 鸿蒙应用质量提升案例-稳定性测试常见JS_ERROR问题分析与定位

harmony 鸿蒙如何通过显示动画实现书籍翻页动效

harmony 鸿蒙如何实现波纹进度条

harmony 鸿蒙折叠展开动效

harmony 鸿蒙如何实现内容下拉变化

harmony 鸿蒙如何删除多选框选项

harmony 鸿蒙如何为同一组件在不同场景下绑定不同的业务逻辑

harmony 鸿蒙分布式画布流转场景

0  赞