From 5e1a0a833790bbdae0f12f1db7c58006dbfd838e Mon Sep 17 00:00:00 2001 From: ql Date: Fri, 25 Mar 2022 03:15:28 +0000 Subject: [PATCH] update zh-cn/application-dev/ability/stage-ability.md. Signed-off-by: ql --- .../application-dev/ability/stage-ability.md | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/zh-cn/application-dev/ability/stage-ability.md b/zh-cn/application-dev/ability/stage-ability.md index 4d7e85b286..547d211211 100644 --- a/zh-cn/application-dev/ability/stage-ability.md +++ b/zh-cn/application-dev/ability/stage-ability.md @@ -163,12 +163,12 @@ context.startAbility(want, options).then((data) => { }) ``` -### 跨设备启动Ability -跨设备场景下,需指定对端设备deviceId,具体获取接口参照[DeviceManager](https://gitee.com/openharmony/device_manager/blob/master/README_zh.md)。具体示例代码如下: +### 跨设备启动Ability(当前仅对系统应用开放) +跨设备场景下,需指定对端设备deviceId,具体示例代码如下: ```ts let context = this.context var want = { - "deviceId": "remoteDeviceId", + "deviceId": "getRemoteDeviceId()", "bundleName": "com.example.MyApplication", "abilityName": "MainAbility" }; @@ -178,6 +178,22 @@ context.startAbility(want).then((data) => { console.error("Failed to start remote ability with error: "+ JSON.stringify(error)) }) ``` +从DeviceManager获取指定设备的deviceId,具体示例代码如下: +```ts +function getRemoteDeviceId() { + if (typeof dmClass === 'object' && dmClass != null) { + var list = dmClass.getTrustedDeviceListSync(); + if (typeof (list) == 'undefined' || typeof (list.length) == 'undefined') { + console.log("MainAbility onButtonClick getRemoteDeviceId err: list is null"); + return; + } + console.log("MainAbility onButtonClick getRemoteDeviceId success:" + list[0].deviceId); + return list[0].deviceId; + } else { + console.log("MainAbility onButtonClick getRemoteDeviceId err: dmClass is null"); + } +} +``` ### 应用向用户申请授权 应用需要某些权限如存储、位置信息、访问日历时,需要向用户申请授权。具体示例代码如下: -- GitLab