From d4b6290c37aa0010598c0f6342cec55b7fa36213 Mon Sep 17 00:00:00 2001 From: ql Date: Thu, 24 Mar 2022 02:33:02 +0000 Subject: [PATCH] update zh-cn/application-dev/ability/stage-ability.md. Signed-off-by: ql --- .../application-dev/ability/stage-ability.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/zh-cn/application-dev/ability/stage-ability.md b/zh-cn/application-dev/ability/stage-ability.md index 7356a8bad4..856bcfaaed 100644 --- a/zh-cn/application-dev/ability/stage-ability.md +++ b/zh-cn/application-dev/ability/stage-ability.md @@ -163,6 +163,22 @@ context.startAbility(want, options).then((data) => { }) ``` +### 跨设备启动Ability +应用可以通过`this.context`获取Ability实例的上下文,获取进而使用AbilityContext中的StartAbility相关接口跨设备启动Ability,所需的对端设备deviceId可通过DeviceManager获取。跨设备启动Ability需在组网认证,并指定对端设备deviceId,具体获取接口参照[DeviceManager](https://gitee.com/openharmony/device_manager/blob/master/README_zh.md)。具体示例代码如下: +```ts +let context = this.context +var want = { + "deviceId": "remoteDeviceId", + "bundleName": "com.example.MyApplication", + "abilityName": "MainAbility" +}; +context.startAbility(want).then((data) => { + console.log("Succeed to start remote ability with data: " + JSON.stringify(data)) +}).catch((error) => { + console.error("Failed to start remote ability with error: "+ JSON.stringify(error)) +}) +``` + ### 应用向用户申请授权 应用需要某些权限如存储、位置信息、访问日历时,需要向用户申请授权。具体示例代码如下: ```ts @@ -174,6 +190,16 @@ context.requestPermissionsFromUser(permissions).then((data) => { console.log("Failed to request permission from user with error: "+ JSON.stringify(error)) }) ``` +在跨设备场景下,需要向用户申请数据同步的权限。具体示例代码如下: +```ts +let context = this.context +let permissions = ohos.permission.DISTRIBUTED_DATASYNC +context.requestPermissionsFromUser(permissions).then((data) => { + console.log("Succeed to request permission from user with data: "+ JSON.stringify(data)) +}).catch((error) => { + console.log("Failed to request permission from user with error: "+ JSON.stringify(error)) +}) +``` ### 系统环境变化通知给AbilityStage及Ability 全局配置,比如系统语言和颜色模式发生变化时,通过onConfigurationUpdated接口通知给AbilityStage和Ability。如下示例展示了AbilityStage的onConfigurationUpdated回调实现,系统语言和颜色模式发生变化时触发该回调。具体示例代码如下: -- GitLab