private context = getContext(this) as common.UIAbilityContext;
private callbackId: number = 0; // 注册订阅系统环境变化的ID

subscribeConfigurationUpdate(): void {
let systemLanguage: string | undefined = this.context.config.language; // 获取系统当前语言

// 1.获取ApplicationContext
let applicationContext = this.context.getApplicationContext();

// 2.通过applicationContext订阅环境变量变化
let environmentCallback: EnvironmentCallback = {
onConfigurationUpdated(newConfig: Configuration) {
hilog.info(DOMAIN_NUMBER, TAG, onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)});
if (this.systemLanguage !== newConfig.language) {
hilog.info(DOMAIN_NUMBER, TAG, systemLanguage from ${systemLanguage} changed to ${newConfig.language});
systemLanguage = newConfig.language; // 将变化之后的系统语言保存,作为下一次变化前的系统语言
}
},
onMemoryLevel(level) {
hilog.info(DOMAIN_NUMBER, TAG, onMemoryLevel level: ${level});
}
}
this.callbackId = applicationContext.on(‘environment’, environmentCallback);
}

// 页面展示
build() {
//…
}
}

  1. 在资源使用完成之后,可以通过调用ApplicationContext.off(type: 'environment', callbackId: number)方法释放相关资源。

import common from ‘@ohos.app.ability.common’;

@Entry
@Component
struct Index {
private context = getContext(this) as common.UIAbilityContext;
private callbackId: number = 0; // 注册订阅系统环境变化的ID

unsubscribeConfigurationUpdate() {
let applicationContext = this.context.getApplicationContext();
applicationContext.off(‘environment’, this.callbackId);
}

// 页面展示
build() {
//…
}
}

在AbilityStage组件容器中订阅回调

使用 AbilityStage.onConfigurationUpdate() 回调方法订阅系统环境变量的变化。当系统环境变量发生变化时,会调用该回调方法。在该方法中,通过 Configuration 对象获取最新的系统环境配置信息。可以进行相应的界面适配等操作,从而提高系统的灵活性和可维护性。

说明:

  • DevEco Studio默认工程中未自动生成AbilityStage,AbilityStage文件的创建请参见 AbilityStage组件容器 。
  • 当使用回调方法订阅系统环境变量的变化时,该回调方法会随着 AbilityStage 的生命周期而存在,在Module销毁时一并销毁。

例如,在 AbilityStage.onConfigurationUpdate() 回调方法中实现监测系统语言的变化。

import AbilityStage from ‘@ohos.app.ability.AbilityStage’;
import hilog from ‘@ohos.hilog’;
import type { Configuration } from ‘@ohos.app.ability.Configuration’;

const TAG: string = ‘[MyAbilityStage]’;
const DOMAIN_NUMBER: number = 0xFF00;

let systemLanguage: string | undefined; // 系统当前语言

export default class MyAbilityStage extends AbilityStage {
onCreate(): void {
systemLanguage = this.context.config.language; // Module首次加载时,获取系统当前语言
hilog.info(DOMAIN_NUMBER, TAG, systemLanguage is ${systemLanguage});
//…
}

onConfigurationUpdate(newConfig: Configuration): void {
hilog.info(DOMAIN_NUMBER, TAG, onConfigurationUpdate, language: ${newConfig.language});
hilog.info(DOMAIN_NUMBER, TAG, onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)});

if (systemLanguage !== newConfig.language) {
hilog.info(DOMAIN_NUMBER, TAG, systemLanguage from ${systemLanguage} changed to ${newConfig.language});
systemLanguage = newConfig.language; // 将变化之后的系统语言保存,作为下一次变化前的系统语言
}
}
}

在UIAbility组件中订阅回调

UIAbility组件提供了UIAbility.onConfigurationUpdate()回调方法用于订阅系统环境变量的变化。当系统环境变量发生变化时,会调用该回调方法。在该方法中,通过 Configuration 对象获取最新的系统环境配置信息,而无需重启UIAbility。

说明:
当使用回调方法订阅系统环境变量的变化时,该回调方法会随着UIAbility的生命周期而存在,在UIAbility销毁时一并销毁。

例如,在onConfigurationUpdate()回调方法中实现监测系统语言的变化。

import AbilityConstant from ‘@ohos.app.ability.AbilityConstant’;
import hilog from ‘@ohos.hilog’;
import UIAbility from ‘@ohos.app.ability.UIAbility’;
import Want from ‘@ohos.app.ability.Want’;
import { Configuration } from ‘@ohos.app.ability.Configuration’;

const TAG: string = ‘[EntryAbility]’;
const DOMAIN_NUMBER: number = 0xFF00;

let systemLanguage: string | undefined; // 系统当前语言

export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
systemLanguage = this.context.config.language; // UIAbility实例首次加载时,获取系统当前语言
hilog.info(DOMAIN_NUMBER, TAG, systemLanguage is ${systemLanguage});
}

onConfigurationUpdate(newConfig: Configuration): void {
hilog.info(DOMAIN_NUMBER, TAG, onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)});

if (systemLanguage !== newConfig.language) {
hilog.info(DOMAIN_NUMBER, TAG, systemLanguage from ${systemLanguage} changed to ${newConfig.language});
systemLanguage = newConfig.language; // 将变化之后的系统语言保存,作为下一次变化前的系统语言
}
}

// …
}

在ExtensionAbility组件中订阅回调

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
img

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
[外链图片转存中…(img-aLbVhjdC-1712669346521)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐