HarmonyOS 如何监听深浅色模式

HarmonyOS
17h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

请参考:

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam,) {
  AppStorage.setOrCreate('currentColorMode',this.context.config.colorMode);
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  let scheme = want.uri?.toString()
  if (scheme?.includes('hotChart')) {
    const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
    router.replaceUrl({
      url: 'pages/TS/List_Sticky' // 目标url
    }, router.RouterMode.Standard, (err) => {
      if (err) {
        console.error(`Invoke replaceUrl failed, code is ${err.code}, message is ${err.message}`);
        return;
      }
      console.info('Invoke replaceUrl succeeded.');
    })
  }
  console.log('MyAbility onCreate')
  let environmentCallback: EnvironmentCallback = {
    onConfigurationUpdated(config){
      AppStorage.setOrCreate('currentColorMode', config.colorMode);
      console.log(`onConfigurationUpdated config: ${JSON.stringify(config)}`);
    },
    onMemoryLevel(level){
      console.log(`onMemoryLevel level: ${level}`);
    }
  };
  // 1.获取applicationContext
  let applicationContext = this.context.getApplicationContext();
  try {
    // 2.通过applicationContext注册监听系统环境变化
    callbackId = applicationContext.on('environment', environmentCallback);
  } catch (paramError) {
    console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
  }
  console.log(`registerEnvironmentCallback callbackId: ${callbackId}`);
}
分享
微博
QQ
微信
回复
14h前
相关问题
HarmonyOS 主动设置深浅色模式
25浏览 • 1回复 待解决
HarmonyOS 浅色模式与深色模式
18浏览 • 0回复 待解决
HarmonyOS 如何设置固定浅色模式
85浏览 • 1回复 待解决
app适配深色浅色切换模式
1900浏览 • 1回复 待解决