一、需求描述
(1)rk3568 OpenHarmony4.0release系统设置没有屏幕睡眠时间的设置,虽然可以通过命令行
命令:power-shell setmode 602
开启不息屏设置,但是重启后,又会进入默认睡眠息屏时间。
(2)模仿手机系统设置应用,可以添加一个自动息屏时间设置,选择设备在一段时间不进行人操作时,进入睡眠息屏设置。
二、案例展示
1、运行条件
(1)rk3568 开发板
(2)系统:OpenHarmony4.0release
2、下载并运行开发案例
下载开发案例,使用Deveco Studio(建议使用4.0.0.600版本)打开运行编译、下载应用到rk3568开发板。
效果如下图所示,点击设置自动息屏,点击弹窗选项,设置自动息屏时间。
(1)在setting中添加自动息屏选项
(2)自动息屏设置(单独demo)
三、开发流程
1、需要申请的权限
(1)将数据项名称及数据项的值保存到数据库中,
需要权限:ohos.permission.MANAGE_SECURE_SETTINGS
2、开发步骤:
(1)导入相关模块
import settings from '@ohos.settings';
(2)设置显示效果的数据项
通过settings.setValue方法,设置相关示效果的数据项,达到设置进入睡眠息屏的时间
//设置系统睡眠息屏30s,不息屏为'-1'
settings.setValue(this.ctx, settings.display.SCREEN_OFF_TIMEOUT, '30000')
.then((status) => {
if (status) {
console.log('Callback return whether value is set.');
} else {
console.log(' fail to set value .');
}
});
(3)自动息屏设置(单独demo)完整代码
import settings from '@ohos.settings';
class ScreenOffTime {
public ScreenOffText: string;
public ScreenOffValue: string;
public isSel: boolean;
public constructor(ScreenOffText: string, ScreenOffValue: string, isSel: boolean) {
this.ScreenOffText = ScreenOffText;
this.ScreenOffValue = ScreenOffValue;
this.isSel = isSel;
}
}
@CustomDialog
export struct CustomDialog_ScreenOff {
@Link ScreenOffTime: ScreenOffTime[]
private controller: CustomDialogController
private ctx: Context = getContext(this);
build() {
Column() {
Text('自动息屏')
.width('100%')
.textAlign(TextAlign.Start)
.padding({ left: 20 })
.fontSize(30)
.margin({ top: 10, bottom: 10 })
Scroll() {
Column() {
ForEach(this.ScreenOffTime, (item: ScreenOffTime, index: number) => {
Row() {
Text(item.ScreenOffText)
.size({ width: '50%', height: '100%' })
.fontSize(25)
Blank();
Radio({ value: item.ScreenOffText, group: 'radioGroup' })
.checked(item.isSel)
.radioStyle({
checkedBackgroundColor: Color.Blue
})
.height(25)
.width(25)
.onChange((isChecked: boolean) => {
settings.setValue(this.ctx, settings.display.SCREEN_OFF_TIMEOUT, item.ScreenOffValue)
.then((status) => {
if (status) {
console.log('Callback return whether value is set.');
} else {
console.log(' fail to set value .');
}
});
this.controller.close();
})
}.size({ width: '100%', height: 30 })
.margin({ top: 5, bottom: 5 })
.padding({ left: 20, right: 20 })
}, (item: ScreenOffTime) => item.ScreenOffText)
}.constraintSize({ minHeight: '100%', maxWidth: '100%' })
}.size({ width: '100%', height: 200 })
}
}
}
@Entry
@Component
struct BrightnessSettings {
private ctx: Context = getContext(this);
@State ScreenOffTime: ScreenOffTime[] =
[new ScreenOffTime("10s", '10000', false),
new ScreenOffTime("30s", '30000', false),
new ScreenOffTime("120s", '120000', false),
new ScreenOffTime("不息屏", '-1', false),
]
@State currentScreenOff: string = '';
private setIntervalID: number = 1;
private ScreenOffController: CustomDialogController = new CustomDialogController({
builder: CustomDialog_ScreenOff({
ScreenOffTime: this.ScreenOffTime,
}),
alignment: DialogAlignment.Bottom,
})
build() {
Column() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
Text('自动息屏')
.fontSize(16)
.lineHeight(22)
.fontWeight(FontWeight.Medium)
.fontColor($r("sys.color.ohos_id_color_text_primary"))
.margin({ left: 8 })
.textAlign(TextAlign.Start);
Row() {
Text(this.currentScreenOff)
.fontSize(14)
.lineHeight(19)
.fontWeight(FontWeight.Regular)
.fontColor($r('sys.color.ohos_id_color_text_primary'))
.opacity($r("sys.float.ohos_fa_alpha_content_secondary"))
.margin({ right: 4 })
.textAlign(TextAlign.End);
Image('/res/image/ic_settings_arrow.svg')
.width(12)
.height(24)
.margin({ right: 8 })
.fillColor($r("sys.color.ohos_id_color_primary"))
.opacity($r("sys.float.ohos_fa_alpha_content_fourth"))
}
}
.padding(4)
.height(56)
.borderRadius(24)
.backgroundColor('#BABAA0')
}
.width('100%')
.height(100)
.onClick(() => {
this.ScreenOffController.open();
})
}
.width('100%')
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center)
.height('100%')
}
aboutToAppear() {
this.setIntervalID = setInterval(async () => {
let value: string = await settings.getValue(this.ctx, settings.display.SCREEN_OFF_TIMEOUT);
for (let i = 0; i < this.ScreenOffTime.length; i++) {
if (this.ScreenOffTime[i].ScreenOffValue === value) {
this.ScreenOffTime[i].isSel = true;
} else {
this.ScreenOffTime[i].isSel = false;
}
}
if (value === '-1') {
this.currentScreenOff = "不息屏"
} else {
let a: number = value.length;
this.currentScreenOff = value.slice(0, a - 3) + 's'
}
console.log(`promise:value -> ${JSON.stringify(value)}`);
}, 300)
}
aboutToDisappear() {
clearInterval(this.setIntervalID);
}
}
四、在setting中添加自动息屏选项
setting源码添加位置:settings\product\phone\src\main\ets\pages\screenAndBrightness.ets页面,添加相应的代码即可,自行添加。