From ad0d714a455bf6cdc7a2cb4dd05dcaa31ae86e03 Mon Sep 17 00:00:00 2001 From: hunili Date: Sat, 26 Aug 2023 09:51:09 +0800 Subject: [PATCH] =?UTF-8?q?arkts=20=E6=8C=87=E5=AF=BC=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E5=8C=96?= =?UTF-8?q?=E6=95=B4=E6=94=B9=20issue:=20https://gitee.com/openharmony/doc?= =?UTF-8?q?s/issues/I7WC28=20Signed-off-by:=20hunili=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hop-cross-device-migration.md | 233 +++++---- .../hop-multi-device-collaboration.md | 459 +++++++++++------- ...s-apis-continuation-continuationManager.md | 240 +++++---- .../apis/js-apis-distributedMissionManager.md | 349 ++++++------- ...-apis-inner-application-continuableInfo.md | 20 +- ...apis-inner-application-continueCallback.md | 15 +- ...is-inner-application-continueDeviceInfo.md | 14 +- ...s-inner-application-continueMissionInfo.md | 15 +- 8 files changed, 779 insertions(+), 566 deletions(-) diff --git a/zh-cn/application-dev/application-models/hop-cross-device-migration.md b/zh-cn/application-dev/application-models/hop-cross-device-migration.md index 84a034344f..dabca884bd 100644 --- a/zh-cn/application-dev/application-models/hop-cross-device-migration.md +++ b/zh-cn/application-dev/application-models/hop-cross-device-migration.md @@ -60,10 +60,10 @@ ```json { "module": { - ... + // ... "abilities": [ { - ... + // ... "continuable": true, } ] @@ -84,20 +84,24 @@ - 迁移决策:开发者可以通过onContinue接口的返回值决定是否支持此次迁移,返回值信息见[接口说明](#接口说明)。 示例如下: - -```ts -import UIAbility from '@ohos.app.ability.UIAbility'; -import AbilityConstant from '@ohos.app.ability.AbilityConstant'; -onContinue(wantParam : {[key: string]: Object}) { - console.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`) //准备迁移数据 - let continueInput = '迁移的数据'; - // 将要迁移的数据保存在wantParam的自定义字段(如:data)中; - wantParam["data"] = continueInput - console.info(`onContinue input = ${wantParam["input"]}`); - return AbilityConstant.OnContinueResult.AGREE -} -``` + ```ts + import UIAbility from '@ohos.app.ability.UIAbility'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + + export default class EntryAbility extends UIAbility { + onContinue(wantParam: Record) { + console.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`) // 准备迁移数据 + let continueInput = '迁移的数据'; + if (continueInput) { + // 将要迁移的数据保存在wantParam的自定义字段(如:data)中; + wantParam["data"] = continueInput; + } + console.info(`onContinue input = ${wantParam["data"]}`); + return AbilityConstant.OnContinueResult.AGREE; + } + } + ``` 5.在Stage模型中,应用在不同启动模式下将调用不同的接口,以恢复数据、加载界面。 @@ -113,42 +117,51 @@ onContinue(wantParam : {[key: string]: Object}) { - 完成数据恢复后,开发者需要调用restoreWindowStage来触发页面恢复:包括页面栈信息。 ```ts - import UIAbility from '@ohos.app.ability.UIAbility'; - import AbilityConstant from '@ohos.app.ability.AbilityConstant'; - - export default class EntryAbility extends UIAbility { - storage : LocalStorage; - onCreate(want, launchParam) { - console.info(`EntryAbility onCreate ${AbilityConstant.LaunchReason.CONTINUATION}`) - if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { - // 将上述的保存的数据取出恢复 - let continueInput = want.parameters.data - console.info(`continue input ${continueInput}`) - // 将数据显示当前页面 - this.storage = new LocalStorage(); - this.context.restoreWindowStage(this.storage); - } - } + import UIAbility from '@ohos.app.ability.UIAbility'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + import Want from '@ohos.app.ability.Want'; + + export default class EntryAbility extends UIAbility { + storage : LocalStorage = new LocalStorage(); + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + console.info(`EntryAbility onCreate ${AbilityConstant.LaunchReason.CONTINUATION}`) + if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { + // 将上述的保存的数据取出恢复 + let continueInput = ''; + if (want.parameters != undefined) { + continueInput = JSON.stringify(want.parameters.data); + console.info(`continue input ${continueInput}`) + } + // 将数据显示当前页面 + this.context.restoreWindowStage(this.storage); + } + } } ``` - 如果是单实例应用,需要额外实现onNewWant接口,实现方式与onCreate的实现相同。 - 在onNewWant中判断迁移场景,恢复数据,并触发页面恢复 - ```ts - export default class EntryAbility extends UIAbility { - storage : LocalStorage; - onNewWant(want, launchParam) { - console.info(`EntryAbility onNewWant ${AbilityConstant.LaunchReason.CONTINUATION}`) - if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { - // get user data from want params - let continueInput = want.parameters.data - console.info(`continue input ${continueInput}`) - this.storage = new LocalStorage(); - this.context.restoreWindowStage(this.storage); - } - } + ```ts + import UIAbility from '@ohos.app.ability.UIAbility'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + import Want from '@ohos.app.ability.Want'; + + export default class EntryAbility extends UIAbility { + storage : LocalStorage = new LocalStorage(); + onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { + console.info(`EntryAbility onNewWant ${AbilityConstant.LaunchReason.CONTINUATION}`) + if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { + // get user data from want params + let continueInput = ''; + if (want.parameters != undefined) { + continueInput = JSON.stringify(want.parameters.data); + console.info(`continue input ${continueInput}`); + } + this.context.restoreWindowStage(this.storage); + } } - ``` + } + ``` @@ -171,12 +184,17 @@ onContinue(wantParam : {[key: string]: Object}) { ```ts // EntryAbility.ets -onCreate(want, launchParam) { - ... - this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { - console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); - }; - ... +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import Want from '@ohos.app.ability.Want'; +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + // ... + this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result) => { + console.info(`setMissionContinueState: ${JSON.stringify(result)}`); + }); + // ... + } } ``` @@ -184,16 +202,21 @@ onCreate(want, launchParam) { ```ts // PageName.ets +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import common from '@ohos.app.ability.common' +@Entry +@Component struct PageName { - Build() { - ... + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... } - ... + // ... onPageShow(){ // 进入该页面时,将应用设置为可迁移状态 this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); - }; + }); } } ``` @@ -201,16 +224,25 @@ struct PageName { 在某个组件的触发事件中设置应用迁移能力。如下例中,使用 **Button** 组件的 **onClick** 事件,触发迁移能力的改变。 ```ts -// xxx.ets -Button(){ - ... +// PageName.ets +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import common from '@ohos.app.ability.common' +@Entry +@Component +struct PageName { + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... + Button() { + //... + }.onClick(()=>{ + //点击该按钮时,将应用设置为可迁移状态 + this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { + console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); + }); + }) + } } -.onClick(()=>{ - //点击该按钮时,将应用设置为可迁移状态 - this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { - console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); - }; -}) ``` **保证迁移连续性** @@ -219,24 +251,28 @@ Button(){ ```ts // EntryAbility.ets -onCreate(want, launchparam) { - ... - // 调用原因为迁移时,设置状态为可迁移,应对冷启动情况 - if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { - this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { - console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); - }; +import UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; +import Want from '@ohos.app.ability.Want'; +export default class EntryAbility extends UIAbility { + // ... + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + // ... + // 调用原因为迁移时,设置状态为可迁移,应对冷启动情况 + this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result) => { + console.info(`setMissionContinueState: ${JSON.stringify(result)}`); + }); } -} - -onNewWant(want, launchparam) { - ... - // 调用原因为迁移时,设置状态为可迁移,应对热启动情况 - if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { - this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { - console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); - }; + onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { + // ... + // 调用原因为迁移时,设置状态为可迁移,应对热启动情况 + if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) { + this.context.setMissionContinueState(AbilityConstant.ContinueState.ACTIVE, (result) => { + console.info('setMissionContinueState ACTIVE result: ', JSON.stringify(result)); + }); + } } + // ... } ``` @@ -251,18 +287,29 @@ onNewWant(want, launchparam) { - 示例:应用迁移不需要自动迁移页面栈信息 ```ts + // EntryAbility.ets import UIAbility from '@ohos.app.ability.UIAbility'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import wantConstant from '@ohos.app.ability.wantConstant'; - - onContinue(wantParam : {[key: string]: Object}) { - console.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`) + import window from '@ohos.window'; + export default class EntryAbility extends UIAbility { + // ... + onContinue(wantParam: Record) { + console.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`); wantParam[wantConstant.Params.SUPPORT_CONTINUE_PAGE_STACK_KEY] = false; return AbilityConstant.OnContinueResult.AGREE; - } - - onWindowStageRestore(windowStage) { - // 若不需要自动迁移页面栈信息,则需要在此处设置应用迁移后进入的页面 - windowStage.setUIContent(this.contex, "pages/index", null); + } + // ... + onWindowStageRestore(windowStage: window.WindowStage) { + // 若不需要自动迁移页面栈信息,则需要在此处设置应用迁移后进入的页面 + windowStage.loadContent('pages/Index', (err, data) => { + if (err.code) { + console.info('Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + console.info('Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); + }); + } } ``` @@ -274,12 +321,16 @@ onNewWant(want, launchparam) { ```ts import UIAbility from '@ohos.app.ability.UIAbility'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import wantConstant from '@ohos.app.ability.wantConstant'; - - onContinue(wantParam : {[key: string]: Object}) { - console.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`) + export default class EntryAbility extends UIAbility { + // ... + onContinue(wantParam: Record) { + console.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`); wantParam[wantConstant.Params.SUPPORT_CONTINUE_SOURCE_EXIT_KEY] = false; return AbilityConstant.OnContinueResult.AGREE; + } + // ... } ``` diff --git a/zh-cn/application-dev/application-models/hop-multi-device-collaboration.md b/zh-cn/application-dev/application-models/hop-multi-device-collaboration.md index 29146ab27c..b5a06df253 100644 --- a/zh-cn/application-dev/application-models/hop-multi-device-collaboration.md +++ b/zh-cn/application-dev/application-models/hop-multi-device-collaboration.md @@ -57,22 +57,22 @@ ```ts import deviceManager from '@ohos.distributedDeviceManager'; - + let dmClass: deviceManager.DeviceManager; function initDmClass() { - // 其中createDeviceManager接口为系统API - try{ - dmClass = deviceManager.createDeviceManager('ohos.samples.demo'); - } catch(err) { - console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message); - } + // 其中createDeviceManager接口为系统API + try{ + dmClass = deviceManager.createDeviceManager('ohos.samples.demo'); + } catch(err) { + console.error("createDeviceManager err: " + JSON.stringify(err)); + } } function getRemoteDeviceId() { if (typeof dmClass === 'object' && dmClass !== null) { let list = dmClass.getAvailableDeviceListSync(); if (typeof (list) === 'undefined' || typeof (list.length) === 'undefined') { - console.info('getRemoteDeviceId err: list is null'); - return; + console.info('getRemoteDeviceId err: list is null'); + return; } if (list.length === 0) { console.info("getRemoteDeviceId err: list is empty"); @@ -81,6 +81,7 @@ return list[0].networkId; } else { console.info('getRemoteDeviceId err: dmClass is null'); + return; } } ``` @@ -88,24 +89,29 @@ 4. 设置目标组件参数,调用[`startAbility()`](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability)接口,启动UIAbility或ServiceExtensionAbility。 ```ts - let want = { - deviceId: getRemoteDeviceId(), + import { BusinessError } from '@ohos.base'; + import Want from '@ohos.app.ability.Want'; + let want: Want = { + deviceId: getRemoteDeviceId(), bundleName: 'com.example.myapplication', abilityName: 'EntryAbility', moduleName: 'entry', // moduleName非必选 } // context为发起端UIAbility的AbilityContext this.context.startAbility(want).then(() => { - ... - }).catch((err) => { - ... + // ... + }).catch((err: BusinessError) => { + // ... + console.error("startAbility err: " + JSON.stringify(err)); }) ``` 5. 当设备A发起端应用不需要设备B上的ServiceExtensionAbility时,可调用[stopServiceExtensionAbility](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstopserviceextensionability)接口退出。(该接口不支持UIAbility的退出,UIAbility由用户手动通过任务管理退出) ```ts - let want = { + import Want from '@ohos.app.ability.Want'; + import { BusinessError } from '@ohos.base'; + let want: Want = { deviceId: getRemoteDeviceId(), bundleName: 'com.example.myapplication', abilityName: 'FuncAbility', @@ -114,7 +120,7 @@ // 退出由startAbility接口启动的ServiceExtensionAbility this.context.stopServiceExtensionAbility(want).then(() => { console.info("stop service extension ability success") - }).catch((err) => { + }).catch((err: BusinessError) => { console.info("stop service extension ability err is " + JSON.stringify(err)) }) ``` @@ -144,55 +150,107 @@ 3. 在发起端设置目标组件参数,调用startAbilityForResult()接口启动目标端UIAbility,异步回调中的data用于接收目标端UIAbility停止自身后返回给调用方UIAbility的信息。getRemoteDeviceId方法参照[通过跨设备启动uiability和serviceextensionability组件实现多端协同无返回数据](#通过跨设备启动uiability和serviceextensionability组件实现多端协同无返回数据)。 ```ts - let want = { - deviceId: getRemoteDeviceId(), - bundleName: 'com.example.myapplication', - abilityName: 'FuncAbility', - moduleName: 'module1', // moduleName非必选 + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; + import common from '@ohos.app.ability.common'; + import { BusinessError } from '@ohos.base'; + import Want from '@ohos.app.ability.Want'; + @Entry + @Component + struct PageName { + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... + Button('StartAbilityForResult') + .onClick(()=>{ + let want: Want = { + deviceId: getRemoteDeviceId(), + bundleName: 'com.example.myapplication', + abilityName: 'FuncAbility', + moduleName: 'module1', // moduleName非必选 + } + // context为发起端UIAbility的AbilityContext + this.context.startAbilityForResult(want).then((data) => { + // ... + }).catch((error: BusinessError) => { + console.info("startAbilityForResult err: " + JSON.stringify(error)); + }) + } + ) + } } - // context为发起端UIAbility的AbilityContext - this.context.startAbilityForResult(want).then((data) => { - ... - }).catch((err) => { - ... - }) ``` 4. 在目标端UIAbility任务完成后,调用terminateSelfWithResult()方法,将数据返回给发起端的UIAbility。 ```ts - const RESULT_CODE: number = 1001; - let abilityResult = { - resultCode: RESULT_CODE, - want: { - bundleName: 'com.example.myapplication', - abilityName: 'FuncAbility', - moduleName: 'module1', - }, + import { BusinessError } from '@ohos.base'; + import common from '@ohos.app.ability.common'; + @Entry + @Component + struct PageName { + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... + Button('terminateSelfWithResult') + .onClick(()=>{ + const RESULT_CODE: number = 1001; + // context为目标端UIAbility的AbilityContext + this.context.terminateSelfWithResult( + { + resultCode: RESULT_CODE, + want: { + bundleName: 'com.example.myapplication', + abilityName: 'FuncAbility', + moduleName: 'module1', + }, + }, + (err: BusinessError) => { + // ... + console.info("terminateSelfWithResult err: " + JSON.stringify(err)); + }); + } + // ... + ) + } } - // context为目标端UIAbility的AbilityContext - this.context.terminateSelfWithResult(abilityResult, (err) => { - ... - }); ``` 5. 发起端UIAbility接收到目标端UIAbility返回的信息,对其进行处理。 ```ts - const RESULT_CODE: number = 1001; - - ... - - // context为调用方UIAbility的UIAbilityContext - this.context.startAbilityForResult(want).then((data) => { - if (data?.resultCode === RESULT_CODE) { - // 解析目标端UIAbility返回的信息 - let info = data.want?.parameters?.info - ... - } - }).catch((err) => { - ... - }) + import common from '@ohos.app.ability.common'; + import { BusinessError } from '@ohos.base'; + import Want from '@ohos.app.ability.Want'; + @Entry + @Component + struct PageName { + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... + Button('StartAbilityForResult') + .onClick(()=>{ + let want: Want = { + deviceId: getRemoteDeviceId(), + bundleName: 'com.example.myapplication', + abilityName: 'FuncAbility', + moduleName: 'module1', // moduleName非必选 + } + const RESULT_CODE: number = 1001; + // ... + // context为调用方UIAbility的UIAbilityContext + this.context.startAbilityForResult(want).then((data) => { + if (data?.resultCode === RESULT_CODE) { + // 解析目标端UIAbility返回的信息 + let info = data.want?.parameters?.info; + // ... + } + }).catch((error: BusinessError) => { + // ... + }) + } + ) + } + } ``` @@ -229,48 +287,60 @@ ```ts import rpc from '@ohos.rpc'; - - const REQUEST_CODE = 99; - let want = { - "deviceId": getRemoteDeviceId(), - "bundleName": "com.example.myapplication", - "abilityName": "ServiceExtAbility" - }; - let options = { - onConnect(elementName, remote) { - console.info('onConnect callback'); - if (remote === null) { - console.info(`onConnect remote is null`); - return; - } - let option = new rpc.MessageOption(); - let data = new rpc.MessageParcel(); - let reply = new rpc.MessageParcel(); - data.writeInt(1); - data.writeInt(99); // 开发者可发送data到目标端应用进行相应操作 - - // @param code 表示客户端发送的服务请求代码。 - // @param data 表示客户端发送的{@link MessageParcel}对象。 - // @param reply 表示远程服务发送的响应消息对象。 - // @param options 指示操作是同步的还是异步的。 - // - // @return 如果操作成功返回{@code true}; 否则返回 {@code false}。 - remote.sendRequest(REQUEST_CODE, data, reply, option).then((ret) => { - let msg = reply.readInt(); // 在成功连接的情况下,会收到来自目标端返回的信息(100) - console.info(`sendRequest ret:${ret} msg:${msg}`); - }).catch((error) => { - console.info('sendRequest failed'); - }); - }, - onDisconnect(elementName) { - console.info('onDisconnect callback'); - }, - onFailed(code) { - console.info('onFailed callback'); + import Want from '@ohos.app.ability.Want'; + import common from '@ohos.app.ability.common'; + import { BusinessError } from '@ohos.base'; + @Entry + @Component + struct PageName { + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... + Button('connectServiceExtensionAbility') + .onClick(()=>{ + const REQUEST_CODE = 99; + let want: Want = { + "deviceId": getRemoteDeviceId(), + "bundleName": "com.example.myapplication", + "abilityName": "ServiceExtAbility" + }; + // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 + let connectionId = this.context.connectServiceExtensionAbility(want, + { + onConnect(elementName, remote) { + console.info('onConnect callback'); + if (remote === null) { + console.info(`onConnect remote is null`); + return; + } + let option = new rpc.MessageOption(); + let data = new rpc.MessageParcel(); + let reply = new rpc.MessageParcel(); + data.writeInt(1); + data.writeInt(99); // 开发者可发送data到目标端应用进行相应操作 + // @param code 表示客户端发送的服务请求代码。 + // @param data 表示客户端发送的{@link MessageParcel}对象。 + // @param reply 表示远程服务发送的响应消息对象。 + // @param options 指示操作是同步的还是异步的。 + // + // @return 如果操作成功返回{@code true}; 否则返回 {@code false}。 + remote.sendRequest(REQUEST_CODE, data, reply, option).then((ret: rpc.SendRequestResult) => { + let msg = reply.readInt(); // 在成功连接的情况下,会收到来自目标端返回的信息(100) + console.info(`sendRequest ret:${ret} msg:${msg}`); + }).catch((error: BusinessError) => { + console.info('sendRequest failed'); + }); + }, + onDisconnect(elementName) { + console.info('onDisconnect callback'); + }, + onFailed(code) { + console.info('onFailed callback'); + } + }); + }) } } - // 建立连接后返回的Id需要保存下来,在解绑服务时需要作为参数传入 - let connectionId = this.context.connectServiceExtensionAbility(want, options); ``` getRemoteDeviceId方法参照[通过跨设备启动uiability和serviceextensionability组件实现多端协同无返回数据](#通过跨设备启动uiability和serviceextensionability组件实现多端协同无返回数据)。 @@ -278,12 +348,25 @@ 5. 断开连接。调用disconnectServiceExtensionAbility()断开与后台服务的连接。 ```ts - let connectionId = 1 // 在通过connectServiceExtensionAbility绑定服务时返回的Id - this.context.disconnectServiceExtensionAbility(connectionId).then((data) => { - console.info('disconnectServiceExtensionAbility success'); - }).catch((error) => { - console.error('disconnectServiceExtensionAbility failed'); - }) + import common from '@ohos.app.ability.common'; + import { BusinessError } from '@ohos.base'; + @Entry + @Component + struct PageName { + private context = getContext(this) as common.UIAbilityContext; + build() { + // ... + Button('disconnectServiceExtensionAbility') + .onClick(()=>{ + let connectionId: number = 1 // 在通过connectServiceExtensionAbility绑定服务时返回的Id + this.context.disconnectServiceExtensionAbility(connectionId).then(() => { + console.info('disconnectServiceExtensionAbility success'); + }).catch((error: BusinessError) => { + console.error('disconnectServiceExtensionAbility failed'); + }) + }) + } + } ``` @@ -342,29 +425,30 @@ 2. 导入UIAbility模块。 ```ts - import Ability from '@ohos.app.ability.UIAbility'; + import UIAbility from '@ohos.app.ability.UIAbility'; ``` 3. 定义约定的序列化数据。 调用端及被调用端发送接收的数据格式需协商一致,如下示例约定数据由number和string组成。 ```ts + import rpc from '@ohos.rpc' export default class MyParcelable { num: number = 0; str: string = ""; - constructor(num, string) { + constructor(num: number, string: string) { this.num = num; this.str = string; } - marshalling(messageSequence) { + marshalling(messageSequence: rpc.MessageParcel) { messageSequence.writeInt(this.num); messageSequence.writeString(this.str); return true; } - unmarshalling(messageSequence) { + unmarshalling(messageSequence: rpc.MessageParcel) { this.num = messageSequence.readInt(); this.str = messageSequence.readString(); return true; @@ -375,24 +459,28 @@ 如下示例在Ability的onCreate注册MSG_SEND_METHOD监听,在onDestroy取消监听,收到序列化数据后作相应处理并返回。应用开发者根据实际业务需要做相应处理。 ```ts + import rpc from '@ohos.rpc'; + import Want from '@ohos.app.ability.Want'; + import UIAbility from '@ohos.app.ability.UIAbility'; + import AbilityConstant from '@ohos.app.ability.AbilityConstant'; const TAG: string = '[CalleeAbility]'; const MSG_SEND_METHOD: string = 'CallSendMsg'; - function sendMsgCallback(data) { + function sendMsgCallback(data: rpc.MessageSequence): MyParcelable { console.info('CalleeSortFunc called'); // 获取Caller发送的序列化数据 - let receivedData = new MyParcelable(0, ''); + let receivedData: MyParcelable = new MyParcelable(0, ''); data.readParcelable(receivedData); console.info(`receiveData[${receivedData.num}, ${receivedData.str}]`); // 作相应处理 // 返回序列化数据result给Caller - return new MyParcelable(receivedData.num + 1, `send ${receivedData.str} succeed`); + return new MyParcelable(Number(receivedData.num) + 1, `send ${receivedData.str} succeed`); } - export default class CalleeAbility extends Ability { - onCreate(want, launchParam) { + export default class CalleeAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { try { this.callee.on(MSG_SEND_METHOD, sendMsgCallback); } catch (error) { @@ -414,42 +502,48 @@ 1. 导入UIAbility模块。 ```ts - import Ability from '@ohos.app.ability.UIAbility'; + import UIAbility from '@ohos.app.ability.UIAbility'; ``` 2. 获取Caller通信接口。 Ability的context属性实现了startAbilityByCall方法,用于获取指定通用组件的Caller通信接口。如下示例通过this.context获取Ability实例的context属性,使用startAbilityByCall拉起Callee被调用端并获取Caller通信接口,注册Caller的onRelease和onRemoteStateChange监听。应用开发者根据实际业务需要做相应处理。 ```ts - async onButtonGetRemoteCaller() { - var caller = undefined; - var context = this.context; - - context.startAbilityByCall({ - deviceId: getRemoteDeviceId(), - bundleName: 'com.samples.CallApplication', - abilityName: 'CalleeAbility' - }).then((data) => { - if (data != null) { - caller = data; - console.info('get remote caller success'); - // 注册caller的release监听 - caller.onRelease((msg) => { - console.info(`remote caller onRelease is called ${msg}`); - }) - console.info('remote caller register OnRelease succeed'); - // 注册caller的协同场景下跨设备组件状态变化监听通知 - try { - caller.onRemoteStateChange((str) => { - console.info('Remote state changed ' + str); - }); - } catch (error) { - console.info('Caller.onRemoteStateChange catch error, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}'); + import UIAbility, { Caller } from '@ohos.app.ability.UIAbility'; + import { BusinessError } from '@ohos.base'; + export default class EntryAbility extends UIAbility { + // ... + async onButtonGetRemoteCaller() { + let caller: Caller | undefined; + let context = this.context; + + context.startAbilityByCall({ + deviceId: getRemoteDeviceId(), + bundleName: 'com.samples.CallApplication', + abilityName: 'CalleeAbility' + }).then((data) => { + if (data != null) { + caller = data; + console.info('get remote caller success'); + // 注册caller的release监听 + caller.onRelease((msg) => { + console.info(`remote caller onRelease is called ${msg}`); + }) + console.info('remote caller register OnRelease succeed'); + // 注册caller的协同场景下跨设备组件状态变化监听通知 + try { + caller.onRemoteStateChange((str) => { + console.info('Remote state changed ' + str); + }); + } catch (error) { + console.info('Caller.onRemoteStateChange catch error, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}'); + } } - } - }).catch((error) => { - console.error(`get remote caller failed with ${error}`); - }) + }).catch((error: BusinessError) => { + console.error(`get remote caller failed with ${error}`); + }) + } + // ... } ``` @@ -459,49 +553,72 @@ 1. 向被调用端发送Parcelable数据有两种方式,一种是不带返回值,一种是获取被调用端返回的数据,method以及序列化数据需要与被调用端协商一致。如下示例调用Call接口,向Callee被调用端发送数据。 ```ts + import UIAbility, { Caller } from '@ohos.app.ability.UIAbility'; + import { BusinessError } from '@ohos.base'; const MSG_SEND_METHOD: string = 'CallSendMsg'; - async onButtonCall() { - try { - let msg = new MyParcelable(1, 'origin_Msg'); - await this.caller.call(MSG_SEND_METHOD, msg); - } catch (error) { - console.info(`caller call failed with ${error}`); - } + export default class EntryAbility extends UIAbility { + // ... + caller: Caller | undefined; + async onButtonCall() { + try { + let msg: MyParcelable = new MyParcelable(1, 'origin_Msg'); + if (this.caller) { + await this.caller.call(MSG_SEND_METHOD, msg); + } + } catch (error) { + console.info(`caller call failed with ${error}`); + } + } + // ... } ``` 2. 如下示例调用CallWithResult接口,向Callee被调用端发送待处理的数据originMsg,并将’CallSendMsg’方法处理完毕的数据赋值给backMsg。 - ```ts - const MSG_SEND_METHOD: string = 'CallSendMsg'; - let originMsg: string = ''; - let backMsg: string = ''; - async onButtonCallWithResult(originMsg, backMsg) { - try { - let msg = new MyParcelable(1, originMsg); - const data = await this.caller.callWithResult(MSG_SEND_METHOD, msg); - console.info('caller callWithResult succeed'); - - let result = new MyParcelable(0, ''); - data.readParcelable(result); - backMsg(result.str); - console.info(`caller result is [${result.num}, ${result.str}]`); - } catch (error) { - console.info(`caller callWithResult failed with ${error}`); - } - } - ``` + ```ts + import UIAbility, { Caller } from '@ohos.app.ability.UIAbility'; + import rpc from '@ohos.rpc'; + const MSG_SEND_METHOD: string = 'CallSendMsg'; + let originMsg: string = ''; + let backMsg: string = ''; + export default class EntryAbility extends UIAbility { + // ... + caller: Caller | undefined; + async onButtonCallWithResult(originMsg: string, backMsg: string) { + try { + let msg: MyParcelable = new MyParcelable(1, originMsg); + if (this.caller) { + const data = await this.caller.callWithResult(MSG_SEND_METHOD, msg); + console.info('caller callWithResult succeed'); + let result: MyParcelable = new MyParcelable(0, ''); + data.readParcelable(result); + backMsg = result.str; + console.info(`caller result is [${result.num}, ${result.str}]`); + } + } catch (error) { + console.info(`caller callWithResult failed with ${error}`); + } + } + // ... + } + ``` 6. 释放Caller通信接口。 Caller不再使用后,应用开发者可以通过release接口释放Caller。 ```ts - releaseCall() { - try { - this.caller.release(); - this.caller = undefined - console.info('caller release succeed'); - } catch (error) { - console.info(`caller release failed with ${error}`); - } + import UIAbility, { Caller } from '@ohos.app.ability.UIAbility'; + export default class EntryAbility extends UIAbility { + caller: Caller | undefined; + releaseCall() { + try { + if (this.caller) { + this.caller.release(); + this.caller = undefined; + } + console.info('caller release succeed'); + } catch (error) { + console.info(`caller release failed with ${error}`); + } + } } ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-continuation-continuationManager.md b/zh-cn/application-dev/reference/apis/js-apis-continuation-continuationManager.md index b2d36361de..3d01e4d692 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-continuation-continuationManager.md +++ b/zh-cn/application-dev/reference/apis/js-apis-continuation-continuationManager.md @@ -43,7 +43,8 @@ register(callback: AsyncCallback\): void; **示例:** ```ts - let token = -1; + import continuationManager from '@ohos.continuation.continuationManager' + let token: number = -1; continuationManager.register((err, data) => { if (err.code != 0) { console.error('register failed, cause: ' + JSON.stringify(err)); @@ -85,17 +86,19 @@ register(options: ContinuationExtraParams, callback: AsyncCallback\): vo **示例:** ```ts - let token = -1; - let continuationExtraParams = { - deviceType: ["00E"] - }; - continuationManager.register(continuationExtraParams, (err, data) => { - if (err.code != 0) { - console.error('register failed, cause: ' + JSON.stringify(err)); - return; - } - console.info('register finished, ' + JSON.stringify(data)); - token = data; + import continuationManager from '@ohos.continuation.continuationManager' + let token: number = -1; + continuationManager.register( + { + deviceType: ["00E"] + }, + (err, data) => { + if (err.code != 0) { + console.error('register failed, cause: ' + JSON.stringify(err)); + return; + } + console.info('register finished, ' + JSON.stringify(data)); + token = data; }); ``` @@ -135,18 +138,16 @@ register(options?: ContinuationExtraParams): Promise\; **示例:** ```ts - let token = -1; - let continuationExtraParams = { - deviceType: ["00E"] - }; - continuationManager.register(continuationExtraParams) - .then((data) => { + import continuationManager from '@ohos.continuation.continuationManager' + import { BusinessError } from '@ohos.base'; + let token: number = -1; + continuationManager.register( + { deviceType: ["00E"] }).then((data) => { console.info('register finished, ' + JSON.stringify(data)); token = data; - }) - .catch((err) => { + }).catch((err: BusinessError) => { console.error('register failed, cause: ' + JSON.stringify(err)); - }); + }); ``` ## continuationManager.registerContinuation9+ @@ -177,7 +178,8 @@ registerContinuation(callback: AsyncCallback\): void; **示例:** ```ts - let token = -1; + import continuationManager from '@ohos.continuation.continuationManager' + let token: number = -1; try { continuationManager.registerContinuation((err, data) => { if (err.code != 0) { @@ -221,18 +223,20 @@ registerContinuation(options: ContinuationExtraParams, callback: AsyncCallback\< **示例:** ```ts - let token = -1; - let continuationExtraParams = { - deviceType: ["00E"] - }; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = -1; try { - continuationManager.registerContinuation(continuationExtraParams, (err, data) => { - if (err.code != 0) { - console.error('registerContinuation failed, cause: ' + JSON.stringify(err)); - return; - } - console.info('registerContinuation finished, ' + JSON.stringify(data)); - token = data; + continuationManager.registerContinuation( + { + deviceType: ["00E"] + }, + (err, data) => { + if (err.code != 0) { + console.error('registerContinuation failed, cause: ' + JSON.stringify(err)); + return; + } + console.info('registerContinuation finished, ' + JSON.stringify(data)); + token = data; }); } catch (err) { console.error('registerContinuation failed, cause: ' + JSON.stringify(err)); @@ -273,19 +277,19 @@ registerContinuation(options?: ContinuationExtraParams): Promise\; **示例:** ```ts - let token = -1; - let continuationExtraParams = { - deviceType: ["00E"] - }; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = -1; try { - continuationManager.register(continuationExtraParams) - .then((data) => { + continuationManager.register( + { + deviceType: ["00E"] + }).then((data) => { console.info('registerContinuation finished, ' + JSON.stringify(data)); token = data; - }) - .catch((err) => { + }).catch((err: BusinessError) => { console.error('registerContinuation failed, cause: ' + JSON.stringify(err)); - }); + }); } catch (err) { console.error('registerContinuation failed, cause: ' + JSON.stringify(err)); } @@ -324,6 +328,7 @@ on(type: "deviceConnect", callback: Callback\): void; **示例:** ```ts + import continuationManager from '@ohos.continuation.continuationManager'; continuationManager.on("deviceConnect", (data) => { console.info('onDeviceConnect deviceId: ' + JSON.stringify(data.id)); console.info('onDeviceConnect deviceType: ' + JSON.stringify(data.type)); @@ -363,6 +368,7 @@ on(type: "deviceDisconnect", callback: Callback\): void; **示例:** ```ts + import continuationManager from '@ohos.continuation.continuationManager'; continuationManager.on("deviceDisconnect", (data) => { console.info('onDeviceDisconnect deviceId: ' + JSON.stringify(data)); }); @@ -400,6 +406,7 @@ off(type: "deviceConnect", callback?: Callback\): void; **示例:** ```ts + import continuationManager from '@ohos.continuation.continuationManager'; continuationManager.off("deviceConnect", (data) => { console.info('onDeviceConnect deviceId: ' + JSON.stringify(data.id)); console.info('onDeviceConnect deviceType: ' + JSON.stringify(data.type)); @@ -439,6 +446,7 @@ off(type: "deviceDisconnect", callback?: Callback\): void; **示例:** ```ts + import continuationManager from '@ohos.continuation.continuationManager'; continuationManager.off("deviceDisconnect", (data) => { console.info('onDeviceDisconnect deviceId: ' + JSON.stringify(data)); }); @@ -475,7 +483,8 @@ on(type: "deviceSelected", token: number, callback: Callback\ { console.info('onDeviceSelected len: ' + data.length); @@ -521,7 +530,8 @@ on(type: "deviceUnselected", token: number, callback: Callback\ { console.info('onDeviceUnselected len: ' + data.length); @@ -567,7 +577,8 @@ off(type: "deviceSelected", token: number): void; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = 1; try { continuationManager.off("deviceSelected", token); } catch (err) { @@ -605,7 +616,8 @@ off(type: "deviceUnselected", token: number): void; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = 1; try { continuationManager.off("deviceUnselected", token); } catch (err) { @@ -646,7 +658,8 @@ startDeviceManager(token: number, callback: AsyncCallback\): void; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = 1; continuationManager.startDeviceManager(token, (err, data) => { if (err.code != 0) { console.error('startDeviceManager failed, cause: ' + JSON.stringify(err)); @@ -690,16 +703,19 @@ startDeviceManager(token: number, options: ContinuationExtraParams, callback: As **示例:** ```ts - let token = 1; - let continuationExtraParams = { - deviceType: ["00E"] - }; - continuationManager.startDeviceManager(token, continuationExtraParams, (err, data) => { - if (err.code != 0) { - console.error('startDeviceManager failed, cause: ' + JSON.stringify(err)); - return; - } - console.info('startDeviceManager finished, ' + JSON.stringify(data)); + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = 1; + continuationManager.startDeviceManager( + token, + { + deviceType: ["00E"] + }, + (err, data) => { + if (err.code != 0) { + console.error('startDeviceManager failed, cause: ' + JSON.stringify(err)); + return; + } + console.info('startDeviceManager finished, ' + JSON.stringify(data)); }); ``` @@ -742,17 +758,18 @@ startDeviceManager(token: number, options?: ContinuationExtraParams): Promise\ { + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = -1; + continuationManager.startDeviceManager( + token, + { + deviceType: ["00E"] + }).then((data) => { console.info('startDeviceManager finished, ' + JSON.stringify(data)); - }) - .catch((err) => { + }).catch((err: BusinessError) => { console.error('startDeviceManager failed, cause: ' + JSON.stringify(err)); - }); + }); ``` ## continuationManager.startContinuationDeviceManager9+ @@ -784,7 +801,8 @@ startContinuationDeviceManager(token: number, callback: AsyncCallback\): v **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = -1; try { continuationManager.startContinuationDeviceManager(token, (err, data) => { if (err.code != 0) { @@ -828,17 +846,20 @@ startContinuationDeviceManager(token: number, options: ContinuationExtraParams, **示例:** ```ts - let token = 1; - let continuationExtraParams = { - deviceType: ["00E"] - }; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = -1; try { - continuationManager.startContinuationDeviceManager(token, continuationExtraParams, (err, data) => { - if (err.code != 0) { - console.error('startContinuationDeviceManager failed, cause: ' + JSON.stringify(err)); - return; - } - console.info('startContinuationDeviceManager finished, ' + JSON.stringify(data)); + continuationManager.startContinuationDeviceManager( + token, + { + deviceType: ["00E"] + }, + (err, data) => { + if (err.code != 0) { + console.error('startContinuationDeviceManager failed, cause: ' + JSON.stringify(err)); + return; + } + console.info('startContinuationDeviceManager finished, ' + JSON.stringify(data)); }); } catch (err) { console.error('startContinuationDeviceManager failed, cause: ' + JSON.stringify(err)); @@ -880,16 +901,17 @@ startContinuationDeviceManager(token: number, options?: ContinuationExtraParams) **示例:** ```ts - let token = 1; - let continuationExtraParams = { - deviceType: ["00E"] - }; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = -1; try { - continuationManager.startContinuationDeviceManager(token, continuationExtraParams) - .then((data) => { + continuationManager.startContinuationDeviceManager( + token, + { + deviceType: ["00E"] + }).then((data) => { console.info('startContinuationDeviceManager finished, ' + JSON.stringify(data)); - }) - .catch((err) => { + }).catch((err: BusinessError) => { console.error('startContinuationDeviceManager failed, cause: ' + JSON.stringify(err)); }); } catch (err) { @@ -932,7 +954,8 @@ updateConnectStatus(token: number, deviceId: string, status: DeviceConnectState, **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = -1; let deviceId: string = "test deviceId"; continuationManager.updateConnectStatus(token, deviceId, continuationManager.DeviceConnectState.CONNECTED, (err, data) => { if (err.code != 0) { @@ -983,15 +1006,17 @@ updateConnectStatus(token: number, deviceId: string, status: DeviceConnectState) **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = 1; let deviceId: string = "test deviceId"; continuationManager.updateConnectStatus(token, deviceId, continuationManager.DeviceConnectState.CONNECTED) .then((data) => { console.info('updateConnectStatus finished, ' + JSON.stringify(data)); }) - .catch((err) => { + .catch((err: BusinessError) => { console.error('updateConnectStatus failed, cause: ' + JSON.stringify(err)); - }); + }); ``` ## continuationManager.updateContinuationState9+ @@ -1025,7 +1050,8 @@ updateContinuationState(token: number, deviceId: string, status: DeviceConnectSt **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = 1; let deviceId: string = "test deviceId"; try { continuationManager.updateContinuationState(token, deviceId, continuationManager.DeviceConnectState.CONNECTED, (err, data) => { @@ -1076,14 +1102,16 @@ updateContinuationState(token: number, deviceId: string, status: DeviceConnectSt **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = 1; let deviceId: string = "test deviceId"; try { continuationManager.updateContinuationState(token, deviceId, continuationManager.DeviceConnectState.CONNECTED) .then((data) => { console.info('updateContinuationState finished, ' + JSON.stringify(data)); }) - .catch((err) => { + .catch((err: BusinessError) => { console.error('updateContinuationState failed, cause: ' + JSON.stringify(err)); }); } catch (err) { @@ -1122,7 +1150,8 @@ unregister(token: number, callback: AsyncCallback\): void; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + let token: number = 1; continuationManager.unregister(token, (err, data) => { if (err.code != 0) { console.error('unregister failed, cause: ' + JSON.stringify(err)); @@ -1167,14 +1196,15 @@ unregister(token: number): Promise\; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = 1; continuationManager.unregister(token) .then((data) => { console.info('unregister finished, ' + JSON.stringify(data)); - }) - .catch((err) => { + }).catch((err: BusinessError) => { console.error('unregister failed, cause: ' + JSON.stringify(err)); - }); + }); ``` ## continuationManager.unregisterContinuation9+ @@ -1206,7 +1236,9 @@ unregisterContinuation(token: number, callback: AsyncCallback\): void; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = 1; try { continuationManager.unregisterContinuation(token, (err, data) => { if (err.code != 0) { @@ -1254,15 +1286,15 @@ unregisterContinuation(token: number): Promise\; **示例:** ```ts - let token = 1; + import continuationManager from '@ohos.continuation.continuationManager'; + import { BusinessError } from '@ohos.base'; + let token: number = -1; try { - continuationManager.unregisterContinuation(token) - .then((data) => { + continuationManager.unregisterContinuation(token).then((data) => { console.info('unregisterContinuation finished, ' + JSON.stringify(data)); - }) - .catch((err) => { + }).catch((err: BusinessError) => { console.error('unregisterContinuation failed, cause: ' + JSON.stringify(err)); - }); + }); } catch (err) { console.error('unregisterContinuation failed, cause: ' + JSON.stringify(err)); } diff --git a/zh-cn/application-dev/reference/apis/js-apis-distributedMissionManager.md b/zh-cn/application-dev/reference/apis/js-apis-distributedMissionManager.md index 9ec3d73d69..b5461d6122 100755 --- a/zh-cn/application-dev/reference/apis/js-apis-distributedMissionManager.md +++ b/zh-cn/application-dev/reference/apis/js-apis-distributedMissionManager.md @@ -35,34 +35,35 @@ registerMissionListener(parameter: MissionDeviceInfo, options: MissionCallback, **示例:** ```ts - function NotifyMissionsChanged(deviceId) { - console.log('NotifyMissionsChanged deviceId ' + JSON.stringify(deviceId)); + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; + function NotifyMissionsChanged(deviceId: string): void { + console.log('NotifyMissionsChanged deviceId ' + JSON.stringify(deviceId)); } - function NotifySnapshot(deviceId, missionId) { - console.log('NotifySnapshot deviceId ' + JSON.stringify(deviceId)); - console.log('NotifySnapshot missionId ' + JSON.stringify(missionId)); + function NotifySnapshot(deviceId: string, missionId: number): void { + console.log('NotifySnapshot deviceId ' + JSON.stringify(deviceId)); + console.log('NotifySnapshot missionId ' + JSON.stringify(missionId)); } - function NotifyNetDisconnect(deviceId, state) { - console.log('NotifyNetDisconnect deviceId ' + JSON.stringify(deviceId)); - console.log('NotifyNetDisconnect state ' + JSON.stringify(state)); - } - var parameter = { - deviceId: "" - }; - var options = { - notifyMissionsChanged: NotifyMissionsChanged, - notifySnapshot: NotifySnapshot, - notifyNetDisconnect: NotifyNetDisconnect + function NotifyNetDisconnect(deviceId: string, state: number): void { + console.log('NotifyNetDisconnect deviceId ' + JSON.stringify(deviceId)); + console.log('NotifyNetDisconnect state ' + JSON.stringify(state)); } try { - distributedMissionManager.registerMissionListener(parameter, options, (error) => { - if (error.code != 0) { - console.error('registerMissionListener failed, cause: ' + JSON.stringify(error)) - } - console.info('registerMissionListener finished') - }) + distributedMissionManager.registerMissionListener( + { deviceId: "" }, + { + notifyMissionsChanged: NotifyMissionsChanged, + notifySnapshot: NotifySnapshot, + notifyNetDisconnect: NotifyNetDisconnect + }, + (error: BusinessError) => { + if (error.code != 0) { + console.error('registerMissionListener failed, cause: ' + JSON.stringify(error)) + } + console.info('registerMissionListener finished') + }); } catch (error) { - console.error('registerMissionListener failed, cause: ' + JSON.stringify(error)) + console.error('registerMissionListener failed, cause: ' + JSON.stringify(error)) } ``` ## distributedMissionManager.registerMissionListener @@ -91,30 +92,29 @@ registerMissionListener(parameter: MissionDeviceInfo, options: MissionCallback): **示例:** ```ts - function NotifyMissionsChanged(deviceId) { - console.log('NotifyMissionsChanged deviceId ' + JSON.stringify(deviceId)); + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; + function NotifyMissionsChanged(deviceId: string): void { + console.log('NotifyMissionsChanged deviceId ' + JSON.stringify(deviceId)); } - function NotifySnapshot(deviceId, missionId) { - console.log('NotifySnapshot deviceId ' + JSON.stringify(deviceId)); - console.log('NotifySnapshot missionId ' + JSON.stringify(missionId)); + function NotifySnapshot(deviceId: string, missionId: number): void { + console.log('NotifySnapshot deviceId ' + JSON.stringify(deviceId)); + console.log('NotifySnapshot missionId ' + JSON.stringify(missionId)); } - function NotifyNetDisconnect(deviceId, state) { - console.log('NotifyNetDisconnect deviceId ' + JSON.stringify(deviceId)); - console.log('NotifyNetDisconnect state ' + JSON.stringify(state)); - } - var parameter = { - deviceId: "" - }; - var options = { - notifyMissionsChanged: NotifyMissionsChanged, - notifySnapshot: NotifySnapshot, - notifyNetDisconnect: NotifyNetDisconnect + function NotifyNetDisconnect(deviceId: string, state: number): void { + console.log('NotifyNetDisconnect deviceId ' + JSON.stringify(deviceId)); + console.log('NotifyNetDisconnect state ' + JSON.stringify(state)); } try { - distributedMissionManager.registerMissionListener(parameter, options) - .then(data => { + distributedMissionManager.registerMissionListener( + { deviceId: "" }, + { + notifyMissionsChanged: NotifyMissionsChanged, + notifySnapshot: NotifySnapshot, + notifyNetDisconnect: NotifyNetDisconnect + }).then((data: void) => { console.info('registerMissionListener finished, ' + JSON.stringify(data)); - }).catch(error => { + }).catch((error: BusinessError) => { console.error('registerMissionListener failed, cause: ' + JSON.stringify(error)); }) } catch (error) { @@ -142,16 +142,17 @@ unRegisterMissionListener(parameter: MissionDeviceInfo, callback: AsyncCallback& **示例:** ```ts - var parameter = { - deviceId: "" - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.unRegisterMissionListener(parameter, (error) => { - if (error.code != 0) { - console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error)) - } - console.info('unRegisterMissionListener finished') - }) + distributedMissionManager.unRegisterMissionListener( + { deviceId: "" }, + (error: BusinessError) => { + if (error.code != 0) { + console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error)) + } + console.info('unRegisterMissionListener finished') + }) } catch (error) { console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error)) } @@ -182,16 +183,14 @@ unRegisterMissionListener(parameter: MissionDeviceInfo): Promise<void> **示例:** ```ts - var parameter = { - deviceId: "" - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.unRegisterMissionListener(parameter) - .then(data => { - console.info('unRegisterMissionListener finished, ' + JSON.stringify(data)); - }).catch(error => { - console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error)); - }) + distributedMissionManager.unRegisterMissionListener({deviceId: ""}).then(() => { + console.info('unRegisterMissionListener finished successfully'); + }).catch((error: BusinessError) => { + console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error)); + }) } catch (error) { console.error('unRegisterMissionListener failed, cause: ' + JSON.stringify(error)) } @@ -217,20 +216,23 @@ startSyncRemoteMissions(parameter: MissionParameter, callback: AsyncCallback< **示例:** ```ts - var parameter = { - deviceId: "", - fixConflict: false, - tag: 0 - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.startSyncRemoteMissions(parameter, (error) => { - if (error.code != 0) { - console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) - } - console.info('startSyncRemoteMissions finished') - }) + distributedMissionManager.startSyncRemoteMissions( + { + deviceId: "", + fixConflict: false, + tag: 0 + }, + (error: BusinessError) => { + if (error.code != 0) { + console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) + } + console.info('startSyncRemoteMissions finished')} + ) } catch (error) { - console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) + console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) } ``` @@ -259,20 +261,22 @@ startSyncRemoteMissions(parameter: MissionParameter): Promise<void> **示例:** ```ts - var parameter = { - deviceId: "", - fixConflict: false, - tag: 0 - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.startSyncRemoteMissions(parameter) - .then(data => { - console.info('startSyncRemoteMissions finished, ' + JSON.stringify(data)); - }).catch(error => { - console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)); - }) + distributedMissionManager.startSyncRemoteMissions( + { + deviceId: "", + fixConflict: false, + tag: 0 + } + ).then(() => { + console.info('startSyncRemoteMissions finished successfully'); + }).catch((error: BusinessError) => { + console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)); + }) } catch (error) { - console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) + console.error('startSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) } ``` @@ -296,18 +300,21 @@ stopSyncRemoteMissions(parameter: MissionDeviceInfo, callback: AsyncCallback< **示例:** ```ts - var parameter = { - deviceId: "" - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.stopSyncRemoteMissions(parameter, (error) => { - if (error.code != 0) { - console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) - } - console.info('stopSyncRemoteMissions finished') - }) + distributedMissionManager.stopSyncRemoteMissions( + { + deviceId: "" + }, + (error: BusinessError) => { + if (error.code != 0) { + console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) + } + console.info('stopSyncRemoteMissions finished')} + ) } catch (error) { - console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) + console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) } ``` @@ -336,18 +343,19 @@ stopSyncRemoteMissions(parameter: MissionDeviceInfo): Promise<void> **示例:** ```ts - var parameter = { - deviceId: "" - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.stopSyncRemoteMissions(parameter) - .then(data => { - console.info('stopSyncRemoteMissions finished, ' + JSON.stringify(data)); - }).catch(error => { - console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)); - }) + distributedMissionManager.stopSyncRemoteMissions( + { + deviceId: "" + }).then((data: void) => { + console.info('stopSyncRemoteMissions finished successfully'); + }).catch((error: BusinessError) => { + console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)); + }) } catch (error) { - console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) + console.error('stopSyncRemoteMissions failed, cause: ' + JSON.stringify(error)) } ``` @@ -385,27 +393,28 @@ continueMission(parameter: ContinueDeviceInfo, options: ContinueCallback, callba **示例:** ```ts - var parameter = { - srcDeviceId: "", - dstDeviceId: "", - missionId: 1, - wantParam: {"key": "value"} - }; - function onContinueDone(resultCode) { - console.log('onContinueDone resultCode: ' + JSON.stringify(resultCode)); - }; - var options = { - onContinueDone: onContinueDone + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; + function onContinueDone(resultCode: number): void { + console.log('onContinueDone resultCode: ' + JSON.stringify(resultCode)); }; try { - distributedMissionManager.continueMission(parameter, options, (error) => { - if (error.code != 0) { - console.error('continueMission failed, cause: ' + JSON.stringify(error)) - } - console.info('continueMission finished') - }) + distributedMissionManager.continueMission( + { + srcDeviceId: "", + dstDeviceId: "", + missionId: 1, + wantParam: {"key": "value"} + }, + { onContinueDone: onContinueDone }, + (error: BusinessError) => { + if (error.code != 0) { + console.error('continueMission failed, cause: ' + JSON.stringify(error)) + } + console.info('continueMission finished') + }) } catch (error) { - console.error('continueMission failed, cause: ' + JSON.stringify(error)) + console.error('continueMission failed, cause: ' + JSON.stringify(error)) } ``` @@ -448,27 +457,26 @@ continueMission(parameter: ContinueDeviceInfo, options: ContinueCallback): Promi **示例:** ```ts - var parameter = { - srcDeviceId: "", - dstDeviceId: "", - missionId: 1, - wantParam: {"key": "value"} - }; - function onContinueDone(resultCode) { - console.log('onContinueDone resultCode: ' + JSON.stringify(resultCode)); - }; - var options = { - onContinueDone: onContinueDone + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; + function onContinueDone(resultCode: number): void { + console.log('onContinueDone resultCode: ' + JSON.stringify(resultCode)); }; try { - distributedMissionManager.continueMission(parameter, options) - .then(data => { - console.info('continueMission finished, ' + JSON.stringify(data)); - }).catch(error => { - console.error('continueMission failed, cause: ' + JSON.stringify(error)); - }) + distributedMissionManager.continueMission( + { + srcDeviceId: "", + dstDeviceId: "", + missionId: 1, + wantParam: {"key": "value"} + }, + { onContinueDone: onContinueDone }).then(() => { + console.info('continueMission finished successfully'); + }).catch((error: BusinessError) => { + console.error('continueMission failed, cause: ' + JSON.stringify(error)); + }) } catch (error) { - console.error('continueMission failed, cause: ' + JSON.stringify(error)) + console.error('continueMission failed, cause: ' + JSON.stringify(error)) } ``` @@ -505,21 +513,24 @@ continueMission(parameter: ContinueMissionInfo, callback: AsyncCallback<void& **示例:** ```ts - var parameter = { - srcDeviceId: "", - dstDeviceId: "", - bundleName: "ohos.test.continueapp", - wantParam: {"key": "value"} - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.continueMission(parameter, (error) => { - if (error.code != 0) { - console.error('continueMission failed, cause: ' + JSON.stringify(error)) - } - console.info('continueMission finished') - }) + distributedMissionManager.continueMission( + { + srcDeviceId: "", + dstDeviceId: "", + bundleName: "ohos.test.continueapp", + wantParam: {"key": "value"} + }, + (error: BusinessError) => { + if (error.code != 0) { + console.error('continueMission failed, cause: ' + JSON.stringify(error)) + } + console.info('continueMission finished') + }) } catch (error) { - console.error('continueMission failed, cause: ' + JSON.stringify(error)) + console.error('continueMission failed, cause: ' + JSON.stringify(error)) } ``` @@ -561,17 +572,19 @@ continueMission(parameter: ContinueMissionInfo): Promise<void> **示例:** ```ts - var parameter = { - srcDeviceId: "", - dstDeviceId: "", - bundleName: "ohos.test.continueapp", - wantParam: {"key": "value"} - }; + import distributedMissionManager from '@ohos.distributedMissionManager' + import { BusinessError } from '@ohos.base'; try { - distributedMissionManager.continueMission(parameter) - .then(data => { - console.info('continueMission finished, ' + JSON.stringify(data)); - }).catch(error => { + distributedMissionManager.continueMission( + { + srcDeviceId: "", + dstDeviceId: "", + bundleName: "ohos.test.continueapp", + wantParam: {"key": "value"} + } + ).then(() => { + console.info('continueMission finished successfully'); + }).catch((error: BusinessError) => { console.error('continueMission failed, cause: ' + JSON.stringify(error)); }) } catch (error) { @@ -599,12 +612,13 @@ on(type: 'continueStateChange', callback: Callback<{ state: ContinueState, i **示例:** ```js + import distributedMissionManager from '@ohos.distributedMissionManager' try { distributedMissionManager.on('continueStateChange', (data) => { console.info("continueStateChange on:" + JSON.stringify(data)); }); - } catch (err) { - console.error("continueStateChange errCode:" + err.code + ",errMessage:" + err.message); + } catch (error) { + console.error("continueStateChange err: " + JSON.stringify(error)); } ``` @@ -628,12 +642,13 @@ off(type: 'continueStateChange', callback?: Callback<{ state: ContinueState, **示例:** ```js + import distributedMissionManager from '@ohos.distributedMissionManager' try { distributedMissionManager.off('continueStateChange', (data) => { - console.info("continueStateChange on:" + JSON.stringify(data)); + console.info("continueStateChange off:" + JSON.stringify(data)); }); } catch (err) { - console.error("continueStateChange errCode:" + err.code + ",errMessage:" + err.message); + console.error("continueStateChange err: " + JSON.stringify(error)); } ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continuableInfo.md b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continuableInfo.md index 9ac8782c1d..b8026fa0c3 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continuableInfo.md +++ b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continuableInfo.md @@ -25,13 +25,13 @@ import distributedMissionManager from '@ohos.distributedMissionManager'; **示例:** ```js - import distributedMissionManager from '@ohos.distributedMissionManager'; - - try { - distributedMissionManager.on('continueStateChange', (data) => { - console.info("continueStateChange on:" + JSON.stringify(data)); - }); - } catch (err) { - console.error("continueStateChange errCode:" + err.code + ",errMessage:" + err.message); - } - ``` \ No newline at end of file +import distributedMissionManager from '@ohos.distributedMissionManager'; + +try { + distributedMissionManager.on('continueStateChange', (data) => { + console.info("continueStateChange on:" + JSON.stringify(data)); + }); +} catch (err) { + console.error("continueStateChange err: " + JSON.stringify(err)); +} +``` \ No newline at end of file diff --git a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueCallback.md b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueCallback.md index 04d0ea50b8..6f31e046aa 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueCallback.md +++ b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueCallback.md @@ -26,26 +26,25 @@ Mission迁移完成后调用,返回迁移结果。 ```ts import distributedMissionManager from '@ohos.distributedMissionManager'; + import { BusinessError } from '@ohos.base'; - let continueDeviceInfo = { + distributedMissionManager.continueMission( + { srcDeviceId: '123', dstDeviceId: '456', missionId: 123, wantParam: { 'key':'value' } - }; - - let continueCallback = { + }, + { onContinueDone(result) { console.log('onContinueDone, result: ${JSON.stringify(result)}'); } - }; - - distributedMissionManager.continueMission(continueDeviceInfo, continueCallback, (error) => { + }, (error: BusinessError) => { if (error && error.code) { console.error('continueMission failed, error.code: ${error.code}, error.message: ${error.message}'); } console.log('continueMission finished'); - }); + }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueDeviceInfo.md b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueDeviceInfo.md index 55f071816d..7f45e2086a 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueDeviceInfo.md +++ b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueDeviceInfo.md @@ -24,22 +24,20 @@ ```ts import distributedMissionManager from '@ohos.distributedMissionManager'; - let continueDeviceInfo = { + distributedMissionManager.continueMission( + { srcDeviceId: '123', dstDeviceId: '456', missionId: 123, wantParam: { 'key':'value' } - }; - - let continueCallback = { - onContinueDone(result) { + }, + { + onContinueDone(result): void { console.log('onContinueDone, result: ${JSON.stringify(result)}'); } - }; - - distributedMissionManager.continueMission(continueDeviceInfo, continueCallback, (error) => { + }, (error) => { if (error && error.code) { console.error('continueMission failed, error.code: ${error.code}, error.message: ${error.message}'); } diff --git a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueMissionInfo.md b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueMissionInfo.md index 453cbb1039..4c6532be27 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueMissionInfo.md +++ b/zh-cn/application-dev/reference/apis/js-apis-inner-application-continueMissionInfo.md @@ -28,15 +28,16 @@ import distributedMissionManager from '@ohos.distributedMissionManager'; ```ts import distributedMissionManager from '@ohos.distributedMissionManager'; + import { BusinessError } from '@ohos.base'; - var parameter = { - srcDeviceId: "", - dstDeviceId: "", - bundleName: "ohos.test.continueapp", - wantParam: {"key": "value"} - }; try { - distributedMissionManager.continueMission(parameter, (error) => { + distributedMissionManager.continueMission( + { + srcDeviceId: "", + dstDeviceId: "", + bundleName: "ohos.test.continueapp", + wantParam: {"key": "value"} + }, (error: BusinessError) => { if (error.code != 0) { console.error('continueMission failed, cause: ' + JSON.stringify(error)) } -- GitLab