From 682efaa7388f87866750f26d55b3487b66f63ded Mon Sep 17 00:00:00 2001 From: dy_study Date: Sat, 2 Jul 2022 11:04:54 +0800 Subject: [PATCH] =?UTF-8?q?IssueNo:#I5F65X=20Description:=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=BA=94=E7=94=A8=E4=B8=8A=E4=B8=8B=E6=96=87=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E7=9B=91=E5=90=AC=E6=8E=A5=E5=8F=A3=E8=B5=84=E6=96=99?= =?UTF-8?q?=20Sig:SIG=5FApplicaitonFramework=20Feature=20or=20Bugfix:Bugfi?= =?UTF-8?q?x=20Binary=20Source:No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy_study Change-Id: I6ccaa6b3f31215480811193e3ee1597b3f5e4c96 --- ...js-apis-application-EnvironmentCallback.md | 62 +++++++++++++ .../js-apis-application-applicationContext.md | 89 +++++++++++++++++-- 2 files changed, 145 insertions(+), 6 deletions(-) create mode 100755 zh-cn/application-dev/reference/apis/js-apis-application-EnvironmentCallback.md diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-EnvironmentCallback.md b/zh-cn/application-dev/reference/apis/js-apis-application-EnvironmentCallback.md new file mode 100755 index 0000000000..fca004b708 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-application-EnvironmentCallback.md @@ -0,0 +1,62 @@ +# EnvironmentCallback + +EnvironmentCallback模块提供应用上下文ApplicationContext对系统环境变化监听回调的能力,包括onConfigurationUpdated方法。 + +> **说明:** +> +> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 +> 本模块接口仅可在Stage模型下使用。 + + +## 导入模块 + +```js +import EnvironmentCallback from "@ohos.application.EnvironmentCallback"; +``` + + +## EnvironmentCallback.onConfigurationUpdated + +onConfigurationUpdated(config: Configuration): void; + +注册监听系统环境变化后,在系统环境变化时触发回调。 + +**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore + +**参数:** + + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | config | [Configuration](js-apis-configuration.md) | 是 | 变化后的Configuration对象 | + +**示例:** + + + ```js +import AbilityStage from "@ohos.application.AbilityStage"; + +var callbackId; + +export default class MyAbilityStage extends AbilityStage { + onCreate() { + console.log("MyAbilityStage onCreate") + globalThis.applicationContext = this.context.getApplicationContext(); + let EnvironmentCallback = { + onConfigurationUpdated(config){ + console.log("onConfigurationUpdated config:" + JSON.stringify(config)); + }, + } + // 1.获取applicationContext + let applicationContext = globalThis.applicationContext; + // 2.通过applicationContext注册监听应用内生命周期 + callbackId = applicationContext.registerEnvironmentCallback(EnvironmentCallback); + console.log("registerEnvironmentCallback number: " + JSON.stringify(callbackId)); + } + onDestroy() { + let applicationContext = globalThis.applicationContext; + applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => { + console.log("unregisterEnvironmentCallback success, err: " + JSON.stringify(error)); + }); + } +} + ``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-applicationContext.md b/zh-cn/application-dev/reference/apis/js-apis-application-applicationContext.md index b47dbf1b74..e9659a9e4a 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-application-applicationContext.md +++ b/zh-cn/application-dev/reference/apis/js-apis-application-applicationContext.md @@ -47,7 +47,7 @@ registerAbilityLifecycleCallback(callback: AbilityLifecycleCallback): **number** ```js import AbilityStage from "@ohos.application.AbilityStage"; -var lifecycleid; +var lifecycleId; export default class MyAbilityStage extends AbilityStage { onCreate() { @@ -78,18 +78,19 @@ export default class MyAbilityStage extends AbilityStage { // 1.通过context属性获取applicationContext let applicationContext = this.context.getApplicationContext(); // 2.通过applicationContext注册监听应用内生命周期 - lifecycleid = applicationContext.registerAbilityLifecycleCallback(AbilityLifecycleCallback); - console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleid)); + lifecycleId = applicationContext.registerAbilityLifecycleCallback(AbilityLifecycleCallback); + console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleId)); } onDestroy() { let applicationContext = this.context.getApplicationContext(); - applicationContext.unregisterAbilityLifecycleCallback(lifecycleid, (error, data) => { + applicationContext.unregisterAbilityLifecycleCallback(lifecycleId, (error, data) => { console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error)); }); } } ``` + ## ApplicationContext.unregisterAbilityLifecycleCallback unregisterAbilityLifecycleCallback(callbackId: **number**, callback: AsyncCallback<**void**>): **void**; @@ -109,9 +110,85 @@ unregisterAbilityLifecycleCallback(callbackId: **number**, callback: AsyncCallb ```js let applicationContext = this.context.getApplicationContext(); - let lifecycleid=1 + let lifecycleId = 1; console.log("stage applicationContext: " + JSON.stringify(applicationContext)); - applicationContext.unregisterAbilityLifecycleCallback(lifecycleid, (error, data) => { + applicationContext.unregisterAbilityLifecycleCallback(lifecycleId, (error, data) => { console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error)); }); ``` + +## ApplicationContext.registerEnvironmentCallback + +registerEnvironmentCallback(callback: EnvironmentCallback): **number**; + +注册监听系统环境变化 + +**系统能力**:SystemCapability.Ability.AbilityRuntime.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------------------------ | -------- | ---- | ------------------------------ | +| [EnvironmentCallback](js-apis-application-EnvironmentCallback.md) | callback | 是 | 回调方法,返回注册监听事件的id | + +**返回值:** + +| 类型 | 说明 | +| ------ | ------------------------------ | +| number | 返回的此次注册监听系统环境变化的id(每次注册该id会自增+1,当超过监听上限数量2^63-1时,返回-1)| + +**示例:** + + ```js +import AbilityStage from "@ohos.application.AbilityStage"; + +var callbackId; + +export default class MyAbilityStage extends AbilityStage { + onCreate() { + console.log("MyAbilityStage onCreate") + globalThis.applicationContext = this.context.getApplicationContext(); + let EnvironmentCallback = { + onConfigurationUpdated(config){ + console.log("onConfigurationUpdated config:" + JSON.stringify(config)); + }, + } + // 1.获取applicationContext + let applicationContext = globalThis.applicationContext; + // 2.通过applicationContext注册监听应用内生命周期 + callbackId = applicationContext.registerEnvironmentCallback(EnvironmentCallback); + console.log("registerEnvironmentCallback number: " + JSON.stringify(callbackId)); + } + onDestroy() { + let applicationContext = globalThis.applicationContext; + applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => { + console.log("unregisterEnvironmentCallback success, err: " + JSON.stringify(error)); + }); + } +} + ``` + +## ApplicationContext.unregisterEnvironmentCallback + +unregisterEnvironmentCallback(callbackId: **number**, callback: AsyncCallback<**void**>): **void**; + +取消监听系统环境变化 + +**系统能力**:SystemCapability.Ability.AbilityRuntime.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------------- | -------- | ---- | -------------------------- | +| callbackId | number | 是 | 注册监听系统环境变化的id | +| AsyncCallback | callback | 是 | 回调方法 | + +**示例:** + + ```js + let applicationContext = this.context.getApplicationContext(); + let callbackId = 1; + applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => { + console.log("unregisterEnvironmentCallback success, err: " + JSON.stringify(error)); + }); + ``` -- GitLab