HarmonyOS 订阅系统环境变量的变化

100 篇文章 0 订阅
87 篇文章 0 订阅

系统环境变量是指:在应用程序运行期间,终端设备的系统设置(例如系统的语言环境、屏幕方向等)发生变化。

开发者通过订阅系统环境变化,可以使应用程序及时感知这种变化,并作出相应处理,从而提供更好的用户体验。例如,用户更改系统语言设置时,应用程序可以自动根据新的语言设置更新用户界面的语言;当用户将设备旋转到横屏或者竖屏时,应用程序可以重新布局用户界面,以适应屏幕方向和尺寸。

系统配置的变化通常由“设置”中的选项或“控制中心”中的图标触发。订阅系统环境变量变化,可以使应用程序更加智能地响应系统环境变化,从而提供更好的用户体验。查看当前支持订阅变化的系统环境变量 。

基于当前的应用模型,可以通过以下几种方式来实现订阅系统环境变量的变化。

  • 使用ApplicationContext订阅回调
  • 在AbilityStage组件容器中订阅回调
  • 在UIAbility组件中订阅回调
  • 在ExtensionAbility组件中订阅回调

使用ApplicationContext订阅回调

ApplicationContext 提供了注册回调函数以订阅系统环境变量的变化,并且可以通过调用相应的方法来撤销该回调。这有助于在资源不再需要时释放相关资源,从而提高系统的可靠性和性能。

  1. 使用ApplicationContext.on(type: 'environment', callback: EnvironmentCallback)方法,应用程序可以通过在非应用组件模块中订阅系统环境变量的变化来动态响应这些变化。例如,使用该方法在页面中监测系统语言的变化。
import common from '@ohos.app.ability.common';
import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
import hilog from '@ohos.hilog';
import { Configuration } from '@ohos.app.ability.Configuration';

const TAG: string = '[CollaborateAbility]';
const DOMAIN_NUMBER: number = 0xFF00;

@Entry
@Component
struct Index {
  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组件中订阅回调

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

说明:

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

以FormExtensionAbility为例说明。例如,在onConfigurationUpdate()回调方法中实现系统环境变量的变化。

import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
import { Configuration } from '@ohos.app.ability.Configuration';

export default class EntryFormAbility extends FormExtensionAbility {
  onConfigurationUpdate(newConfig: Configuration) {
    console.info(`newConfig is ${JSON.stringify(newConfig)}`);
  }

  // ...
}

总是有很多小伙伴反馈说:鸿蒙开发不知道学习哪些技术?不知道需要重点掌握哪些鸿蒙应用开发知识点? 为了解决大家这些学习烦恼。在这准备了一份很实用的鸿蒙(HarmonyOS NEXT)学习路线与学习文档给大家用来跟着学习。

针对一些列因素,整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、WebGL、元服务、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植等等)鸿蒙(HarmonyOS NEXT)技术知识点。

《鸿蒙 (Harmony OS)开发学习手册》(共计892页):https://gitcode.com/HarmonyOS_MN

如何快速入门?

1.基本概念
2.构建第一个ArkTS应用
3.……


在这里插入图片描述

开发基础知识

1.应用基础知识
2.配置文件
3.应用数据管理
4.应用安全管理
5.应用隐私保护
6.三方应用调用管控机制
7.资源分类与访问
8.学习ArkTS语言
9.……

基于ArkTS 开发

1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……

鸿蒙开发面试真题(含参考答案):https://gitcode.com/HarmonyOS_MN

OpenHarmony 开发环境搭建:https://gitcode.com/HarmonyOS_MN


在这里插入图片描述

《OpenHarmony源码解析》:https://gitcode.com/HarmonyOS_MN

搭建开发环境
系统架构分析

  • 构建子系统
  • 启动流程
  • 子系统
  • 分布式任务调度子系统
  • 分布式通信子系统
  • 驱动子系统
  • ……

OpenHarmony 设备开发学习手册:https://gitcode.com/HarmonyOS_MN

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值