![](https://s5-media.51cto.com/ost/pc/static/noavatar.gif)
#2023盲盒+码# 电源管理应用开发指导 原创
【本文正在参加 2023「盲盒」+码有奖征文活动】 https://ost.51cto.com/posts/25284
@toc
关于作者:I’m westinyang
序言
测试设备:OpenHarmony 3.2 Release (API9)
本文讲解如何开发一个支持、关机、重启、锁屏以及电源模式切换的电源管理应用,在部分开发板或其它移植的设备上,软件提供的关机重启可能比物理按钮更方便,另外由于原版系统设置中并没有提供电源模式,电源模式切换为性能模式,可以使设备屏幕常亮,防止连接设备调测的时候总是过一会就锁屏的问题。需要用到 @ohos.power
模块,部分接口需要system_basic级别的系统权限。
前置环境
- 需要用到Full-SDK,可以参考官方文档自行编译,或者在 http://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist 这里下载
- Full-SDK替换指南:https://docs.openharmony.cn/pages/v3.2/zh-cn/application-dev/quick-start/full-sdk-switch-guide.md/
项目权限配置
entry/src/main/module.json5
"requestPermissions": [
{
"name": "ohos.permission.REBOOT"
},
{
"name": "ohos.permission.POWER_OPTIMIZATION"
}
],
访问控制权限修改
openharmony-sdk\9\toolchains\lib\UnsgnedReleasedProfileTemplate.json
"apl":"system_basic",
"app-feature":"hos_system_app"
开发步骤
状态定义和初始化
电源模式的切换支持4种模式,我们先定义电源模式的状态和对应的中文描述,在主页面显示时,先使用 power.getPowerMode() 获取当前系统的电源模式,用于页面展示中初始化选中。
@State powerModeKey: number = 600
@State powerModeVal: string = '标准模式'
@State radioItemWidth: number = 0
aboutToAppear() {
let powerMode: power.DevicePowerMode = power.getPowerMode()
if (powerMode == power.DevicePowerMode.MODE_NORMAL) {
this.powerModeKey = 600;
this.powerModeVal = '标准模式';
} else if (powerMode == power.DevicePowerMode.MODE_POWER_SAVE) {
this.powerModeKey = 601;
this.powerModeVal = '省电模式';
} else if (powerMode == power.DevicePowerMode.MODE_PERFORMANCE) {
this.powerModeKey = 602;
this.powerModeVal = '性能模式';
} else if (powerMode == power.DevicePowerMode.MODE_EXTREME_POWER_SAVE) {
this.powerModeKey = 603;
this.powerModeVal = '超级省电模式';
}
}
主要布局代码
这里使用Row横向均分放置三个按钮,下方为多个PowerModeItem自定义组件组合,用于显示电源模式单选框组
@Builder MainContent() {
Row() {
Button('关机').width('30%').backgroundColor('#c0392b').onClick(()=>{
try {
power.shutdown('shutdown');
} catch(err) {
console.error('shutdown failed, err: ' + err);
}
})
Button('重启').width('30%').backgroundColor('#27ae60').onClick(()=>{
try {
power.reboot('reboot');
} catch(err) {
console.error('reboot failed, err: ' + err);
}
})
Button('锁屏').width('30%').backgroundColor('#34495e').onClick(()=>{
try {
power.suspend();
} catch(err) {
console.error('suspend failed, err: ' + err);
}
})
}
.width('100%')
.justifyContent(FlexAlign.SpaceBetween)
.margin({bottom: 15})
// 电源模式
Column() {
Text('电源模式').fontSize(20).fontColor('#fff').fontWeight(500).margin(15)
this.PowerModeItem(power.DevicePowerMode.MODE_NORMAL, 600, '标准模式')
this.PowerModeItem(power.DevicePowerMode.MODE_POWER_SAVE, 601, '省电模式')
this.PowerModeItem(power.DevicePowerMode.MODE_PERFORMANCE, 602, '性能模式')
this.PowerModeItem(power.DevicePowerMode.MODE_EXTREME_POWER_SAVE, 603, '超级省电模式', false)
}.width('100%').backgroundColor('#3c3f41').padding({left: 10, right: 10, top: 5, bottom: 5}).borderRadius(20).margin({bottom: 15})
}
电源模式项具体实现
每一个行都用到了Radia单选框组件,并且监听onChange事件,用于处理电源模式的切换,使用 power.setPowerMode 来修改系统的电源模式。
@Builder PowerModeItem(thisMode: power.DevicePowerMode, thisKey: number, thisVal: string, showDivider: boolean = true) {
Row() {
Radio({ value: 'Radio1', group: 'radioGroup' }).checked(this.powerModeKey == thisKey).height(25).width(25)
.responseRegion({
width: this.radioItemWidth,
height: '100%'
})
.onChange((isChecked: boolean) => {
if (isChecked) {
power.setPowerMode(thisMode)
.then(() => {
this.powerModeKey = thisKey;
this.powerModeVal = thisVal;
promptAction.showToast({ message: '电源模式设置为:' + thisVal, duration: 1000, });
})
.catch(err => {
console.error('set power mode failed, err: ' + err);
});
}
})
Text(thisVal + (thisKey == 600 ? '(默认)': '')).fontColor('#fff').responseRegion({ width: 0, height: 0 }).margin({left: 5})
}
.width('100%')
.height(50)
if (showDivider) {
Divider().width('100%').strokeWidth(0.5).color('#6d6d6d').padding({left: 5, right: 5})
}
}
开源地址和截图预览
https://gitee.com/ohos-dev/power-mgt 完整的实现和布局代码,可以参考项目开源仓库
总结
本文可以帮助你学习如何使用 @ohos.power
模块提供的功能,获取和设置电源模式,设置屏幕状态以及重启关机等操作的实现。
持续关注
- 关于作者:I’m westinyang
- 哔哩哔哩:个人主页
![](https://s5-media.51cto.com/ost/pc/static/noavatar.gif)