From 749849307c449c0f121724c649cef32e6f062a49 Mon Sep 17 00:00:00 2001 From: HuangXW Date: Sat, 22 Oct 2022 20:52:27 +0800 Subject: [PATCH] =?UTF-8?q?Context=E9=94=99=E8=AF=AF=E7=A0=81=E6=95=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: HuangXW --- zh-cn/application-dev/ability/stage-call.md | 10 +- .../reference/apis/js-apis-ability-context.md | 1136 ++++++++++++++--- .../apis/js-apis-application-ability.md | 353 +++-- .../apis/js-apis-extension-context.md | 4 +- .../apis/js-apis-service-extension-context.md | 912 +++++++++++-- 5 files changed, 2001 insertions(+), 414 deletions(-) diff --git a/zh-cn/application-dev/ability/stage-call.md b/zh-cn/application-dev/ability/stage-call.md index 7aa9769c0a..f7cdca7a28 100644 --- a/zh-cn/application-dev/ability/stage-call.md +++ b/zh-cn/application-dev/ability/stage-call.md @@ -40,7 +40,7 @@ Caller及Callee功能如下:具体的API详见[接口文档](../reference/apis |call(method: string, data: rpc.Sequenceable): Promise\|向通用组件Callee发送约定序列化数据。| |callWithResult(method: string, data: rpc.Sequenceable): Promise\|向通用组件Callee发送约定序列化数据, 并将Callee返回的约定序列化数据带回。| |release(): void|释放通用组件的Caller通信接口。| -|onRelease(callback: OnReleaseCallBack): void|注册通用组件通信断开监听通知。| +|on(type: "release", callback: OnReleaseCallback): void|注册通用组件通信断开监听通知。| ## 开发步骤 Call调用的开发步骤: @@ -72,7 +72,7 @@ Ability配置标签示例如下: ``` **2. 导入Ability模块** ```ts -import Ability from '@ohos.application.Ability' +import Ability from '@ohos.app.ability.Ability' ``` **3. 定义约定的序列化数据** @@ -142,7 +142,7 @@ export default class CalleeAbility extends Ability { ### 访问Callee被调用端 **1. 导入Ability模块** ```ts -import Ability from '@ohos.application.Ability' +import Ability from '@ohos.app.ability.Ability' ``` **2. 获取Caller通信接口** @@ -151,7 +151,7 @@ import Ability from '@ohos.application.Ability' // 注册caller的release监听 private regOnRelease(caller) { try { - caller.onRelease((msg) => { + caller.on("release", (msg) => { console.log(`caller onRelease is called ${msg}`) }) console.log('caller register OnRelease succeed') @@ -192,7 +192,7 @@ async onButtonGetRemoteCaller() { caller = data console.log('get remote caller success') // 注册caller的release监听 - caller.onRelease((msg) => { + caller.on("release", (msg) => { console.log(`remote caller onRelease is called ${msg}`) }) console.log('remote caller register OnRelease succeed') diff --git a/zh-cn/application-dev/reference/apis/js-apis-ability-context.md b/zh-cn/application-dev/reference/apis/js-apis-ability-context.md index 4b64eed416..b624f9b191 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-ability-context.md +++ b/zh-cn/application-dev/reference/apis/js-apis-ability-context.md @@ -14,10 +14,12 @@ AbilityContext模块提供允许访问特定Ability的资源的能力,包括 在使用AbilityContext的功能前,需要通过Ability子类实例获取。 ```js -import Ability from '@ohos.application.Ability'; +import Ability from '@ohos.app.ability.Ability'; + + let context = undefined; class MainAbility extends Ability { onWindowStageCreate(windowStage) { - let context = this.context; + context = this.context; } } ``` @@ -47,17 +49,56 @@ startAbility(want: Want, callback: AsyncCallback<void>): void; | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | callback | AsyncCallback<void> | 是 | callback形式返回启动结果 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js var want = { - "deviceId": "", - "bundleName": "com.extreme.test", - "abilityName": "MainAbility" + "bundleName": "com.example.myapp", + "abilityName": "MyAbility" }; - this.context.startAbility(want, (error) => { - console.log("error.code = " + error.code) - }) + + try { + this.context.startAbility(want, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -77,22 +118,61 @@ startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void& | options | [StartOptions](js-apis-application-StartOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js var want = { - "deviceId": "", - "bundleName": "com.extreme.test", - "abilityName": "MainAbility" + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "MainAbility" }; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbility(want, options, (error) => { - console.log("error.code = " + error.code) - }) - ``` + try { + this.context.startAbility(want, options, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } + ``` ## AbilityContext.startAbility @@ -115,23 +195,59 @@ startAbility(want: Want, options?: StartOptions): Promise<void>; | -------- | -------- | | Promise<void> | Promise形式返回启动结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js var want = { - "deviceId": "", - "bundleName": "com.extreme.test", - "abilityName": "MainAbility" + "bundleName": "com.example.myapp", + "abilityName": "MyAbility" }; var options = { windowMode: 0, }; - this.context.startAbility(want, options) - .then(() => { - console.log('Operation successful.') - }).catch((error) => { - console.log('Operation failed.'); - }) + + try { + this.context.startAbility(want, options) + .then((data) => { + // 执行正常业务 + console.log('startAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -150,17 +266,58 @@ startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): | want |[Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | 是 | 执行结果回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | **示例:** ```js - this.context.startAbilityForResult( - {deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, - (error, result) => { - console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code) - console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode) - } - ); + var want = { + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "MainAbility" + }; + + try { + this.context.startAbilityForResult(want, (error, result) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log("startAbilityForResult succeed, result.resultCode = " + + result.resultCode) + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.startAbilityForResult @@ -179,20 +336,61 @@ startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback | options | [StartOptions](js-apis-application-StartOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | 是 | 执行结果回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | **示例:** ```js + var want = { + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "MainAbility" + }; var options = { windowMode: 0, }; - this.context.startAbilityForResult( - {deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options, - (error, result) => { - console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code) - console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode) - } - ); + + try { + this.context.startAbilityForResult(want, options, (error, result) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log("startAbilityForResult succeed, result.resultCode = " + + result.resultCode) + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -218,17 +416,59 @@ startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityRes | -------- | -------- | | Promise<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Promise形式返回执行结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js + var want = { + "bundleName": "com.example.myapp", + "abilityName": "MyAbility" + }; var options = { - windowMode: 0, + windowMode: 0, }; - this.context.startAbilityForResult({deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options).then((result) => { - console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode) - }, (error) => { - console.log("startAbilityForResult Promise.Reject is called, error.code = " + error.code) - }) + + try { + this.context.startAbilityForResult(want, options) + .then((result) => { + // 执行正常业务 + console.log("startAbilityForResult succeed, result.resultCode = " + result.resultCode); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.startAbilityForResultWithAccount @@ -251,6 +491,33 @@ startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncC | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数,返回Ability结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -260,10 +527,24 @@ startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncC "abilityName": "MainAbility" }; var accountId = 100; - this.context.startAbilityWithAccount(want, accountId, (err, data) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - console.log('---------- startAbilityWithAccount success, data: -----------', data); - }); + + try { + this.context.startAbilityForResultWithAccount(want, accountId, (error, result) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log("startAbilityForResultWithAccount succeed, result.resultCode = " + + result.resultCode) + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -288,6 +569,33 @@ startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOp | options | [StartOptions](js-apis-application-StartOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -298,11 +606,26 @@ startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOp }; var accountId = 100; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbilityForResultWithAccount(want, accountId, options, (err) => { - console.log('---------- startAbilityForResultWithAccount fail, err: -----------', err); - }); + + try { + this.context.startAbilityForResultWithAccount(want, accountId, options, (error, result) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log("startAbilityForResultWithAccount succeed, result.resultCode = " + + result.resultCode) + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -332,6 +655,33 @@ startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartO | -------- | -------- | | Promise<AbilityResult> | 返回一个Promise,包含Ability结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -342,15 +692,26 @@ startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartO }; var accountId = 100; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbilityForResultWithAccount(want, accountId, options) - .then((data) => { - console.log('---------- startAbilityForResultWithAccount success, data: -----------', data); - }) - .catch((err) => { - console.log('---------- startAbilityForResultWithAccount fail, err: -----------', err); - }) + + try { + this.context.startAbilityForResultWithAccount(want, accountId, options) + .then((result) => { + // 执行正常业务 + console.log("startAbilityForResultWithAccount succeed, result.resultCode = " + + result.resultCode) + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.startServiceExtensionAbility @@ -369,6 +730,25 @@ startServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -377,9 +757,23 @@ startServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.startServiceExtensionAbility(want, (err) => { - console.log('---------- startServiceExtensionAbility fail, err: -----------', err); - }); + + try { + this.context.startServiceExtensionAbility(want, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startServiceExtensionAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.startServiceExtensionAbility @@ -398,6 +792,25 @@ startServiceExtensionAbility(want: Want): Promise\; | -------- | -------- | -------- | -------- | | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -406,14 +819,25 @@ startServiceExtensionAbility(want: Want): Promise\; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.startServiceExtensionAbility(want) - .then(() => { - console.log('---------- startServiceExtensionAbility success -----------'); - }) - .catch((err) => { - console.log('---------- startServiceExtensionAbility fail, err: -----------', err); - }) + + try { + this.context.startServiceExtensionAbility(want) + .then((data) => { + // 执行正常业务 + console.log('startServiceExtensionAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` + ## AbilityContext.startServiceExtensionAbilityWithAccount startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\): void; @@ -434,6 +858,22 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -443,9 +883,23 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: "abilityName": "MainAbility" }; var accountId = 100; - this.context.startServiceExtensionAbilityWithAccount(want,accountId, (err) => { - console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startServiceExtensionAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.startServiceExtensionAbilityWithAccount @@ -467,6 +921,26 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\ | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -476,13 +950,23 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\ "abilityName": "MainAbility" }; var accountId = 100; - this.context.startServiceExtensionAbilityWithAccount(want,accountId) - .then(() => { - console.log('---------- startServiceExtensionAbilityWithAccount success -----------'); - }) - .catch((err) => { - console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err); - }) + + try { + this.context.startServiceExtensionAbilityWithAccount(want, accountId) + .then((data) => { + // 执行正常业务 + console.log('startServiceExtensionAbilityWithAccount succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.stopServiceExtensionAbility @@ -501,6 +985,22 @@ stopServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -509,9 +1009,23 @@ stopServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.stopServiceExtensionAbility(want, (err) => { - console.log('---------- stopServiceExtensionAbility fail, err: -----------', err); - }); + + try { + this.context.stopServiceExtensionAbility(want, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('stopServiceExtensionAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.stopServiceExtensionAbility @@ -530,6 +1044,22 @@ stopServiceExtensionAbility(want: Want): Promise\; | -------- | -------- | -------- | -------- | | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -538,13 +1068,23 @@ stopServiceExtensionAbility(want: Want): Promise\; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.stopServiceExtensionAbility(want) - .then(() => { - console.log('---------- stopServiceExtensionAbility success -----------'); - }) - .catch((err) => { - console.log('---------- stopServiceExtensionAbility fail, err: -----------', err); - }) + + try { + this.context.stopServiceExtensionAbility(want) + .then((data) => { + // 执行正常业务 + console.log('stopServiceExtensionAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.stopServiceExtensionAbilityWithAccount @@ -567,6 +1107,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -576,9 +1133,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: "abilityName": "MainAbility" }; var accountId = 100; - this.context.stopServiceExtensionAbilityWithAccount(want,accountId, (err) => { - console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('stopServiceExtensionAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.stopServiceExtensionAbilityWithAccount @@ -600,6 +1171,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\< | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -609,13 +1197,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\< "abilityName": "MainAbility" }; var accountId = 100; - this.context.stopServiceExtensionAbilityWithAccount(want,accountId) - .then(() => { - console.log('---------- stopServiceExtensionAbilityWithAccount success -----------'); - }) - .catch((err) => { - console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err); - }) + + try { + this.context.stopServiceExtensionAbilityWithAccount(want, accountId) + .then((data) => { + // 执行正常业务 + console.log('stopServiceExtensionAbilityWithAccount succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.terminateSelf @@ -632,11 +1230,31 @@ terminateSelf(callback: AsyncCallback<void>): void; | -------- | -------- | -------- | -------- | | callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - this.context.terminateSelf((err) => { - console.log('terminateSelf result:' + JSON.stringify(err)); + this.context.terminateSelf((error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('terminateSelf succeed'); }); ``` @@ -655,13 +1273,29 @@ terminateSelf(): Promise<void>; | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - this.context.terminateSelf().then(() => { - console.log('success'); + this.context.terminateSelf().then((data) => { + // 执行正常业务 + console.log('terminateSelf succeed'); }).catch((error) => { - console.log('failed:' + JSON.stringify(error)); + // 处理业务逻辑错误 + console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); }); ``` @@ -740,9 +1374,9 @@ terminateSelfWithResult(parameter: AbilityResult): Promise<void>; ) ``` -## AbilityContext.connectAbility +## AbilityContext.connectServiceExtensionAbility -connectAbility(want: Want, options: ConnectOptions): number; +connectServiceExtensionAbility(want: Want, options: ConnectOptions): number; 使用AbilityInfo.AbilityType.SERVICE模板将当前Ability连接到一个Ability。 @@ -763,6 +1397,20 @@ connectAbility(want: Want, options: ConnectOptions): number; | -------- | -------- | | number | 返回Ability连接的结果code。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -776,14 +1424,21 @@ connectAbility(want: Want, options: ConnectOptions): number; onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, onFailed(code) { console.log('----------- onFailed -----------') } } - const result = this.context.connectAbility(want, options); - console.log('----------- connectAbilityResult: ------------', result); + + var connection = null; + try { + connection = this.context.connectServiceExtensionAbility(want, options); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## AbilityContext.connectAbilityWithAccount +## AbilityContext.connectServiceExtensionAbilityWithAccount -connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; +connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; 使用AbilityInfo.AbilityType.SERVICE模板和account将当前Ability连接到一个Ability。 @@ -807,6 +1462,21 @@ connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions | -------- | -------- | | number | 返回Ability连接的结果code。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000006 | Can not cross user operations. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -821,13 +1491,20 @@ connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, onFailed(code) { console.log('----------- onFailed -----------') } } - const result = this.context.connectAbilityWithAccount(want, accountId, options); - console.log('----------- connectAbilityResult: ------------', result); + + var connection = null; + try { + connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## AbilityContext.disconnectAbility +## AbilityContext.disconnectServiceExtensionAbility -disconnectAbility(connection: number): Promise\; +disconnectServiceExtensionAbility(connection: number): Promise\; 断开连接(promise形式)。 @@ -847,20 +1524,46 @@ disconnectAbility(connection: number): Promise\; | -------- | -------- | | Promise\ | 返回执行结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000003 | Input error. The specified id does not exist. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - var connectionNumber = 0; - this.context.disconnectAbility(connectionNumber).then(() => { - console.log('disconnectAbility success'); - }).catch((err) => { - console.log('disconnectAbility fail, err: ', err); - }); + // connection为connectAbility中的返回值 + var connection = 1; + + try { + this.context.disconnectServiceExtensionAbility(connection) + .then((data) => { + // 执行正常业务 + console.log('disconnectServiceExtensionAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## AbilityContext.disconnectAbility +## AbilityContext.disconnectServiceExtensionAbility -disconnectAbility(connection: number, callback:AsyncCallback\): void; +disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\): void; 断开连接(callback形式)。 @@ -875,13 +1578,41 @@ disconnectAbility(connection: number, callback:AsyncCallback\): void; | connection | number | 是 | 连接的Ability的数字代码。 | | callback | AsyncCallback\ | 是 | 表示指定的回调方法。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000003 | Input error. The specified id does not exist. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - var connectionNumber = 0; - this.context.disconnectAbility(connectionNumber, (err) => { - console.log('---------- disconnectAbility fail, err: -----------', err); + // connection为connectServiceExtensionAbility中的返回值 + var connection = 1; + + try { + this.context.disconnectServiceExtensionAbility(connection, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('disconnectServiceExtensionAbility succeed'); }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.startAbilityByCall @@ -907,7 +1638,7 @@ startAbilityByCall(want: Want): Promise<Caller>; **示例:** ```js - let caller = undefined; + var caller = undefined; // 后台启动Ability,不配置parameters var wantBackground = { @@ -963,6 +1694,33 @@ startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\< | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -972,9 +1730,23 @@ startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\< "abilityName": "MainAbility" }; var accountId = 100; - this.context.startAbilityWithAccount(want, accountId, (err) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.startAbilityWithAccount(want, accountId, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -999,6 +1771,33 @@ startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, ca | options | [StartOptions](js-apis-application-StartOptions.md) | 否 | 启动Ability所携带的参数。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -1009,11 +1808,25 @@ startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, ca }; var accountId = 100; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbilityWithAccount(want, accountId, options, (err) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.startAbilityWithAccount(want, accountId, options, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -1037,6 +1850,33 @@ startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | options | [StartOptions](js-apis-application-StartOptions.md) | 否 | 启动Ability所携带的参数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -1047,15 +1887,25 @@ startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): }; var accountId = 100; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbilityWithAccount(want, accountId, options) - .then(() => { - console.log('---------- startAbilityWithAccount success -----------'); - }) - .catch((err) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - }) + + try { + this.context.startAbilityWithAccount(want, accountId, options) + .then((data) => { + // 执行正常业务 + console.log('startAbilityWithAccount succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## AbilityContext.requestPermissionsFromUser diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-ability.md b/zh-cn/application-dev/reference/apis/js-apis-application-ability.md index 1c67e86ee5..fd01ae6eb1 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-application-ability.md +++ b/zh-cn/application-dev/reference/apis/js-apis-application-ability.md @@ -45,7 +45,7 @@ Ability创建时回调,执行初始化业务逻辑操作。 | param | AbilityConstant.LaunchParam | 是 | 创建 ability、上次异常退出的原因信息。 | **示例:** - + ```js class myAbility extends Ability { onCreate(want, param) { @@ -320,7 +320,6 @@ onMemoryLevel(level: AbilityConstant.MemoryLevel): void; 通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。 - ## Caller.call call(method: string, data: rpc.Sequenceable): Promise<void>; @@ -342,54 +341,67 @@ call(method: string, data: rpc.Sequenceable): Promise<void>; | -------- | -------- | | Promise<void> | Promise形式返回应答。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16200001 | Caller released. The caller has been released. | +| 16200002 | Callee invalid. The callee does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - import Ability from '@ohos.application.Ability'; + import Ability from '@ohos.app.ability.Ability'; class MyMessageAble{ // 自定义的Sequenceable数据结构 - name:"" - str:"" - num: 1 - constructor(name, str) { - this.name = name; - this.str = str; - } - marshalling(messageParcel) { - messageParcel.writeInt(this.num); - messageParcel.writeString(this.str); - console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); - return true; - } - unmarshalling(messageParcel) { - this.num = messageParcel.readInt(); - this.str = messageParcel.readString(); - console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); - return true; - } + name:"" + str:"" + num: 1 + constructor(name, str) { + this.name = name; + this.str = str; + } + marshalling(messageParcel) { + messageParcel.writeInt(this.num); + messageParcel.writeString(this.str); + console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + unmarshalling(messageParcel) { + this.num = messageParcel.readInt(); + this.str = messageParcel.readString(); + console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } }; var method = 'call_Function'; // 约定的通知消息字符串 var caller; export default class MainAbility extends Ability { - onWindowStageCreate(windowStage) { - this.context.startAbilityByCall({ - bundleName: "com.example.myservice", - abilityName: "MainAbility", - deviceId: "" - }).then((obj) => { - caller = obj; - let msg = new MyMessageAble(1, "world"); // 参考Sequenceable数据定义 - caller.call(method, msg) - .then(() => { - console.log('Caller call() called'); - }).catch((e) => { - console.log('Caller call() catch error ' + e); - }); - console.log('Caller GetCaller Get ' + caller); - }).catch((e) => { - console.log('Caller GetCaller error ' + e); - }); - } - + onWindowStageCreate(windowStage) { + this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "MainAbility", + deviceId: "" + }).then((obj) => { + caller = obj; + let msg = new MyMessageAble(1, "world"); // 参考Sequenceable数据定义 + caller.call(method, msg) + .then(() => { + console.log('Caller call() called'); + }) + .catch((callErr) => { + console.log('Caller.call catch error, error.code: ' + JSON.stringify(callErr.code) + + ' error.message: ' + JSON.stringify(callErr.message)); + }); + }).catch((err) => { + console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } } ``` @@ -415,55 +427,69 @@ callWithResult(method: string, data: rpc.Sequenceable): Promise<rpc.MessagePa | -------- | -------- | | Promise<rpc.MessageParcel> | Promise形式返回通用组件服务端应答数据。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16200001 | Caller released. The caller has been released. | +| 16200002 | Callee invalid. The callee does not exist. | +| 16000050 | Internal Error. | + **示例:** - + ```js - import Ability from '@ohos.application.Ability'; + import Ability from '@ohos.app.ability.Ability'; class MyMessageAble{ - name:"" - str:"" - num: 1 - constructor(name, str) { - this.name = name; - this.str = str; - } - marshalling(messageParcel) { - messageParcel.writeInt(this.num); - messageParcel.writeString(this.str); - console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); - return true; - } - unmarshalling(messageParcel) { - this.num = messageParcel.readInt(); - this.str = messageParcel.readString(); - console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); - return true; - } + name:"" + str:"" + num: 1 + constructor(name, str) { + this.name = name; + this.str = str; + } + marshalling(messageParcel) { + messageParcel.writeInt(this.num); + messageParcel.writeString(this.str); + console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + unmarshalling(messageParcel) { + this.num = messageParcel.readInt(); + this.str = messageParcel.readString(); + console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } }; var method = 'call_Function'; var caller; export default class MainAbility extends Ability { - onWindowStageCreate(windowStage) { - this.context.startAbilityByCall({ - bundleName: "com.example.myservice", - abilityName: "MainAbility", - deviceId: "" - }).then((obj) => { - caller = obj; - let msg = new MyMessageAble(1, "world"); - caller.callWithResult(method, msg) - .then((data) => { - console.log('Caller callWithResult() called'); - let retmsg = new MyMessageAble(0, ""); - data.readSequenceable(retmsg); - }).catch((e) => { - console.log('Caller callWithResult() catch error ' + e); - }); - console.log('Caller GetCaller Get ' + caller); - }).catch((e) => { - console.log('Caller GetCaller error ' + e); - }); - } + onWindowStageCreate(windowStage) { + this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "MainAbility", + deviceId: "" + }).then((obj) => { + caller = obj; + let msg = new MyMessageAble(1, "world"); + caller.callWithResult(method, msg) + .then((data) => { + console.log('Caller callWithResult() called'); + let retmsg = new MyMessageAble(0, ""); + data.readSequenceable(retmsg); + }) + .catch((callErr) => { + console.log('Caller.callWithResult catch error, error.code: ' + JSON.stringify(callErr.code) + + ' error.message: ' + JSON.stringify(callErr.message)); + }); + }).catch((err) => { + console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } } ``` @@ -476,36 +502,48 @@ release(): void; **系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 401 | Invalid input parameter. | +| 16200001 | Caller released. The caller has been released. | +| 16200002 | Callee invalid. The callee does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - import Ability from '@ohos.application.Ability'; + import Ability from '@ohos.app.ability.Ability'; var caller; export default class MainAbility extends Ability { - onWindowStageCreate(windowStage) { - this.context.startAbilityByCall({ - bundleName: "com.example.myservice", - abilityName: "MainAbility", - deviceId: "" - }).then((obj) => { - caller = obj; - try { - caller.release(); - } catch (e) { - console.log('Caller Release error ' + e); - } - console.log('Caller GetCaller Get ' + caller); - }).catch((e) => { - console.log('Caller GetCaller error ' + e); - }); - } + onWindowStageCreate(windowStage) { + this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "MainAbility", + deviceId: "" + }).then((obj) => { + caller = obj; + try { + caller.release(); + } catch (releaseErr) { + console.log('Caller.release catch error, error.code: ' + JSON.stringify(releaseErr.code) + + ' error.message: ' + JSON.stringify(releaseErr.message)); + } + }).catch((err) => { + console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } } ``` -## Caller.onRelease +## Caller.on -onRelease(callback: OnReleaseCallBack): void; + on(type: "release", callback: OnReleaseCallback): void; 注册通用组件服务端Stub(桩)断开监听通知。 @@ -515,33 +553,45 @@ onRelease(callback: OnReleaseCallBack): void; | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | + | type | string | 是 | 监听releaseCall事件,固定为'release'。 | | callback | OnReleaseCallBack | 是 | 返回onRelease回调结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 401 | Invalid input parameter. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js - import Ability from '@ohos.application.Ability'; + import Ability from '@ohos.app.ability.Ability'; var caller; export default class MainAbility extends Ability { - onWindowStageCreate(windowStage) { - this.context.startAbilityByCall({ - bundleName: "com.example.myservice", - abilityName: "MainAbility", - deviceId: "" - }).then((obj) => { - caller = obj; - try { - caller.onRelease((str) => { - console.log(' Caller OnRelease CallBack is called ' + str); - }); - } catch (e) { - console.log('Caller Release error ' + e); - } - console.log('Caller GetCaller Get ' + caller); - }).catch((e) => { - console.log('Caller GetCaller error ' + e); - }); - } + onWindowStageCreate(windowStage) { + this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "MainAbility", + deviceId: "" + }).then((obj) => { + caller = obj; + try { + caller.on("release", (str) => { + console.log(' Caller OnRelease CallBack is called ' + str); + }); + } catch (error) { + console.log('Caller.on catch error, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + } + }).catch((err) => { + console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } } ``` @@ -550,7 +600,6 @@ onRelease(callback: OnReleaseCallBack): void; 通用组件服务端注册和解除客户端caller通知送信的callback接口。 - ## Callee.on on(method: string, callback: CalleeCallBack): void; @@ -566,10 +615,20 @@ on(method: string, callback: CalleeCallBack): void; | method | string | 是 | 与客户端约定的通知消息字符串。 | | callback | CalleeCallBack | 是 | 一个rpc.MessageParcel类型入参的js通知同步回调函数, 回调函数至少要返回一个空的rpc.Sequenceable数据对象, 其他视为函数执行错误。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 401 | Invalid input parameter. | +| 16200004 | Method registered. The method has registered. | +| 16000050 | Internal Error. | + **示例:** - + ```js - import Ability from '@ohos.application.Ability'; + import Ability from '@ohos.app.ability.Ability'; class MyMessageAble{ name:"" str:"" @@ -599,14 +658,18 @@ on(method: string, callback: CalleeCallBack): void; return new MyMessageAble(10, "Callee test"); } export default class MainAbility extends Ability { - onCreate(want, launchParam) { - console.log('Callee onCreate is called'); - this.callee.on(method, funcCallBack); + onCreate(want, launchParam) { + console.log('Callee onCreate is called'); + try { + this.callee.on(method, funcCallBack); + } catch (error) { + console.log('Callee.on catch error, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); } + } } ``` - ## Callee.off off(method: string): void; @@ -621,20 +684,36 @@ off(method: string): void; | -------- | -------- | -------- | -------- | | method | string | 是 | 已注册的通知事件字符串。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 401 | Invalid input parameter. | +| 16200005 | Method not registered. The method has not registered. | +| 16000050 | Internal Error. | + + **示例:** ```js - import Ability from '@ohos.application.Ability'; + import Ability from '@ohos.app.ability.Ability'; var method = 'call_Function'; export default class MainAbility extends Ability { - onCreate(want, launchParam) { - console.log('Callee onCreate is called'); - this.callee.off(method); + onCreate(want, launchParam) { + console.log('Callee onCreate is called'); + try { + this.callee.off(method); + } catch (error) { + console.log('Callee.off catch error, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); } + } } ``` -## OnReleaseCallBack +## OnReleaseCallback (msg: string): void; @@ -644,7 +723,7 @@ off(method: string): void; | -------- | -------- | -------- | -------- | -------- | | (msg: string) | function | 是 | 否 | 调用者注册的侦听器函数接口的原型。 | -## CalleeCallBack +## CalleeCallback (indata: rpc.MessageParcel): rpc.Sequenceable; diff --git a/zh-cn/application-dev/reference/apis/js-apis-extension-context.md b/zh-cn/application-dev/reference/apis/js-apis-extension-context.md index 1425107f97..aab5d3f7ff 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-extension-context.md +++ b/zh-cn/application-dev/reference/apis/js-apis-extension-context.md @@ -31,7 +31,7 @@ ExtensionContext主要用于查询所属Extension的信息、Module的配置信 三个Module内都定义一个相同名称的ServiceExtension: ``` js -import ServiceExtension from '@ohos.application.ServiceExtensionAbility' +import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility' import Want from '@ohos.application.Want' export default class TheServiceExtension extends ServiceExtension { onCreate(want:Want) { @@ -61,7 +61,7 @@ export default class TheServiceExtension extends ServiceExtension { 在entry的MainAbility的onCreate回调内启动ServiceExtension ``` js -import Ability from '@ohos.application.Ability' +import Ability from '@ohos.app.ability.Ability' export default class MainAbility extends Ability { onCreate(want, launchParam) { console.log("[Demo] MainAbility onCreate"); diff --git a/zh-cn/application-dev/reference/apis/js-apis-service-extension-context.md b/zh-cn/application-dev/reference/apis/js-apis-service-extension-context.md index 37d5bcf23c..9d01e2733e 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-service-extension-context.md +++ b/zh-cn/application-dev/reference/apis/js-apis-service-extension-context.md @@ -14,15 +14,17 @@ ServiceExtensionContext模块提供ServiceExtensionAbility具有的能力和接 在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。 ```js - import ServiceExtensionAbility from '@ohos.application.ServiceExtensionAbility'; + import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; + + let context = undefined; class MainAbility extends ServiceExtensionAbility { - onCreate() { - let context = this.context; - } + onCreate() { + context = this.context; + } } ``` -## startAbility +## ServiceExtensionContext.startAbility startAbility(want: Want, callback: AsyncCallback<void>): void; @@ -39,18 +41,59 @@ startAbility(want: Want, callback: AsyncCallback<void>): void; | want | [Want](js-apis-application-Want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | | callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js - let want = { - "bundleName": "com.example.myapp", - "abilityName": "MyAbility"}; - this.context.startAbility(want, (err) => { - console.log('startAbility result:' + JSON.stringify(err)); + var want = { + "bundleName": "com.example.myapp", + "abilityName": "MyAbility" + }; + + try { + this.context.startAbility(want, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbility succeed'); }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## startAbility +## ServiceExtensionContext.startAbility startAbility(want: Want, options?: StartOptions): Promise\; @@ -73,22 +116,62 @@ startAbility(want: Want, options?: StartOptions): Promise\; | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js - let want = { - "bundleName": "com.example.myapp", - "abilityName": "MyAbility" - }; - this.context.startAbility(want).then((data) => { - console.log('success:' + JSON.stringify(data)); - }).catch((error) => { - console.log('failed:' + JSON.stringify(error)); - }); + var want = { + "bundleName": "com.example.myapp", + "abilityName": "MyAbility" + }; + var options = { + windowMode: 0, + }; + try { + this.context.startAbility(want, options) + .then((data) => { + // 执行正常业务 + console.log('startAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## startAbility +## ServiceExtensionContext.startAbility startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void @@ -106,20 +189,60 @@ startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void& | options | [StartOptions](js-apis-application-StartOptions.md) | 是 | 启动Ability所携带的参数。 | | callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** - + ```js var want = { - "deviceId": "", - "bundleName": "com.extreme.test", - "abilityName": "MainAbility" + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "MainAbility" }; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbility(want, options, (error) => { - console.log("error.code = " + error.code) - }) + + try { + this.context.startAbility(want, options, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.startAbilityWithAccount @@ -140,6 +263,33 @@ startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\< | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -149,11 +299,24 @@ startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\< "abilityName": "MainAbility" }; var accountId = 100; - this.context.startAbilityWithAccount(want, accountId, (err) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - }); - ``` + try { + this.context.startAbilityWithAccount(want, accountId, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } + ``` ## ServiceExtensionContext.startAbilityWithAccount @@ -174,6 +337,33 @@ startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, ca | options | [StartOptions](js-apis-application-StartOptions.md) | 否 | 启动Ability所携带的参数。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -184,11 +374,25 @@ startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, ca }; var accountId = 100; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbilityWithAccount(want, accountId, options, (err) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.startAbilityWithAccount(want, accountId, options, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` @@ -216,6 +420,33 @@ startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000010 | Can not operation with continue flag. | +| 16000011 | Context does not exist. | +| 16000051 | Network error. The network is abnormal. | +| 16000052 | Free install not support. The application does not support freeinstall | +| 16000053 | Not top ability. The application is not top ability. | +| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. | +| 16000055 | Free install timeout. | +| 16000056 | Can not free install other ability. | +| 16000057 | Not support cross device free install. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -226,15 +457,25 @@ startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): }; var accountId = 100; var options = { - windowMode: 0, + windowMode: 0 }; - this.context.startAbilityWithAccount(want, accountId, options) - .then((data) => { - console.log('---------- startAbilityWithAccount success, data: -----------', data); - }) - .catch((err) => { - console.log('---------- startAbilityWithAccount fail, err: -----------', err); - }) + + try { + this.context.startAbilityWithAccount(want, accountId, options) + .then((data) => { + // 执行正常业务 + console.log('startAbilityWithAccount succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.startServiceExtensionAbility @@ -254,6 +495,25 @@ startServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; | want | [Want](js-apis-application-Want.md) | 是 | 启动Ability的want信息。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -262,9 +522,23 @@ startServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.startServiceExtensionAbility(want, (err) => { - console.log('---------- startServiceExtensionAbility fail, err: -----------', err); - }); + + try { + this.context.startServiceExtensionAbility(want, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startServiceExtensionAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.startServiceExtensionAbility @@ -289,6 +563,25 @@ startServiceExtensionAbility(want: Want): Promise\; | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -297,13 +590,23 @@ startServiceExtensionAbility(want: Want): Promise\; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.startServiceExtensionAbility(want) - .then((data) => { - console.log('---------- startServiceExtensionAbility success, data: -----------', data); - }) - .catch((err) => { - console.log('---------- startServiceExtensionAbility fail, err: -----------', err); - }) + + try { + this.context.startServiceExtensionAbility(want) + .then((data) => { + // 执行正常业务 + console.log('startServiceExtensionAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.startServiceExtensionAbilityWithAccount @@ -326,6 +629,27 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: | accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 启动Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + + **示例:** ```js @@ -335,9 +659,23 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: "abilityName": "MainAbility" }; var accountId = 100; - this.context.startServiceExtensionAbilityWithAccount(want,accountId, (err) => { - console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('startServiceExtensionAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.startServiceExtensionAbilityWithAccount @@ -365,6 +703,26 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\ | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -374,13 +732,23 @@ startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\ "abilityName": "MainAbility" }; var accountId = 100; - this.context.startServiceExtensionAbilityWithAccount(want,accountId) - .then((data) => { - console.log('---------- startServiceExtensionAbilityWithAccount success, data: -----------', data); - }) - .catch((err) => { - console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err); - }) + + try { + this.context.startServiceExtensionAbilityWithAccount(want, accountId) + .then((data) => { + // 执行正常业务 + console.log('startServiceExtensionAbilityWithAccount succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.stopServiceExtensionAbility @@ -400,6 +768,22 @@ stopServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; | want | [Want](js-apis-application-Want.md) | 是 | 停止Ability的want信息。 | | callback | AsyncCallback\ | 是 | 停止Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -408,9 +792,23 @@ stopServiceExtensionAbility(want: Want, callback: AsyncCallback\): void; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.stopServiceExtensionAbility(want, (err) => { - console.log('---------- stopServiceExtensionAbility fail, err: -----------', err); - }); + + try { + this.context.stopServiceExtensionAbility(want, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('stopServiceExtensionAbility succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.stopServiceExtensionAbility @@ -435,6 +833,22 @@ stopServiceExtensionAbility(want: Want): Promise\; | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -443,13 +857,23 @@ stopServiceExtensionAbility(want: Want): Promise\; "bundleName": "com.extreme.test", "abilityName": "MainAbility" }; - this.context.stopServiceExtensionAbility(want) - .then((data) => { - console.log('---------- stopServiceExtensionAbility success, data: -----------', data); - }) - .catch((err) => { - console.log('---------- stopServiceExtensionAbility fail, err: -----------', err); - }) + + try { + this.context.stopServiceExtensionAbility(want) + .then((data) => { + // 执行正常业务 + console.log('stopServiceExtensionAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount @@ -472,6 +896,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: | accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 | | callback | AsyncCallback\ | 是 | 停止Ability的回调函数。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -481,9 +922,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: "abilityName": "MainAbility" }; var accountId = 100; - this.context.stopServiceExtensionAbilityWithAccount(want,accountId, (err) => { - console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err); - }); + + try { + this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('stopServiceExtensionAbilityWithAccount succeed'); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount @@ -511,6 +966,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\< | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000006 | Can not cross user operations. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16200001 | Caller released. The caller has been released. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -520,13 +992,23 @@ stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\< "abilityName": "MainAbility" }; var accountId = 100; - this.context.stopServiceExtensionAbilityWithAccount(want,accountId) - .then((data) => { - console.log('---------- stopServiceExtensionAbilityWithAccount success, data: -----------', data); - }) - .catch((err) => { - console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err); - }) + + try { + this.context.stopServiceExtensionAbilityWithAccount(want, accountId) + .then((data) => { + // 执行正常业务 + console.log('stopServiceExtensionAbilityWithAccount succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.terminateSelf @@ -545,11 +1027,31 @@ terminateSelf(callback: AsyncCallback<void>): void; | -------- | -------- | -------- | -------- | | callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - this.context.terminateSelf((err) => { - console.log('terminateSelf result:' + JSON.stringify(err)); + this.context.terminateSelf((error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('terminateSelf succeed'); }); ``` @@ -569,19 +1071,35 @@ terminateSelf(): Promise<void>; | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js this.context.terminateSelf().then((data) => { - console.log('success:' + JSON.stringify(data)); + // 执行正常业务 + console.log('terminateSelf succeed'); }).catch((error) => { - console.log('failed:' + JSON.stringify(error)); + // 处理业务逻辑错误 + console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); }); ``` -## ServiceExtensionContext.connectAbility +## ServiceExtensionContext.connectServiceExtensionAbility -connectAbility(want: Want, options: ConnectOptions): number; +connectServiceExtensionAbility(want: Want, options: ConnectOptions): number; 将一个Ability与服务类型的Ability绑定。 @@ -602,24 +1120,46 @@ connectAbility(want: Want, options: ConnectOptions): number; | -------- | -------- | | number | 返回一个number,后续根据这个number去断开连接。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - let want = { + var want = { "bundleName": "com.example.myapp", "abilityName": "MyAbility" }; - let options = { + var options = { onConnect(elementName, remote) { console.log('----------- onConnect -----------') }, onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, onFailed(code) { console.log('----------- onFailed -----------') } } - let connection = this.context.connectAbility(want,options); + + var connection = null; + try { + connection = this.context.connectServiceExtensionAbility(want, options); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## ServiceExtensionContext.connectAbilityWithAccount +## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount -connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; +connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; 使用AbilityInfo.AbilityType.SERVICE模板和account将当前能力连接到一个能力。 @@ -641,6 +1181,21 @@ connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions | -------- | -------- | | number | 返回Ability连接的结果code。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000002 | Ability type error. The specified ability type is wrong. | +| 16000004 | Visibility verification failed. | +| 16000006 | Can not cross user operations. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js @@ -655,13 +1210,20 @@ connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, onFailed(code) { console.log('----------- onFailed -----------') } } - const result = this.context.connectAbilityWithAccount(want, accountId, options); - console.log('----------- connectAbilityResult: ------------', result); + + var connection = null; + try { + connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## ServiceExtensionContext.disconnectAbility +## ServiceExtensionContext.disconnectServiceExtensionAbility -disconnectAbility(connection: number, callback:AsyncCallback<void>): void; +disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback<void>): void; 将一个Ability与绑定的服务类型的Ability解绑。 @@ -676,19 +1238,46 @@ disconnectAbility(connection: number, callback:AsyncCallback<void>): void; | connection | number | 是 | 在connectAbility中返回的number。 | | callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000003 | Input error. The specified id does not exist. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - let connection=1 - this.context.disconnectAbility(connection, (err) => { - // connection为connectAbility中的返回值 - console.log('terminateSelf result:' + JSON.stringify(err)); + // connection为connectServiceExtensionAbility中的返回值 + var connection = 1; + + try { + this.context.disconnectServiceExtensionAbility(connection, (error) => { + if (error.code) { + // 处理业务逻辑错误 + console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + + ' error.message: ' + JSON.stringify(error.message)); + return; + } + // 执行正常业务 + console.log('disconnectServiceExtensionAbility succeed'); }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` -## ServiceExtensionContext.disconnectAbility +## ServiceExtensionContext.disconnectServiceExtensionAbility -disconnectAbility(connection: number): Promise<void>; +disconnectServiceExtensionAbility(connection: number): Promise<void>; 将一个Ability与绑定的服务类型的Ability解绑。通过Promise返回结果。 @@ -707,17 +1296,42 @@ disconnectAbility(connection: number): Promise<void>; | 类型 | 说明 | | -------- | -------- | | Promise<void> | 返回一个Promise,包含接口的结果。 | - + +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000003 | Input error. The specified id does not exist. | +| 16000011 | Context does not exist. | +| 16000050 | Internal Error. | + **示例:** ```js - let connection=1 - this.context.disconnectAbility(connection).then((data) => { - // connection为connectAbility中的返回值 - console.log('success:' + JSON.stringify(data)); - }).catch((error) => { - console.log('failed:' + JSON.stringify(error)); - }); + // connection为connectAbility中的返回值 + var connection = 1; + + try { + this.context.disconnectServiceExtensionAbility(connection) + .then((data) => { + // 执行正常业务 + console.log('disconnectServiceExtensionAbility succeed'); + }) + .catch((error) => { + // 处理业务逻辑错误 + console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` ## ServiceExtensionContext.startAbilityByCall @@ -742,10 +1356,28 @@ startAbilityByCall(want: Want): Promise<Caller>; | -------- | -------- | | Promise<Caller> | 获取要通讯的caller对象。 | +**错误码:** + +以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)。 + +| 错误码ID | 错误信息 | +| ------- | -------------------------------- | +| 201 | The application does not have permission to call the interface. | +| 401 | Invalid input parameter. | +| 16000001 | Input error. The specified ability name does not exist. | +| 16000004 | Visibility verification failed. | +| 16000005 | Static permission denied. The specified process does not have the permission. | +| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. | +| 16000008 | Crowdtest App Expiration. | +| 16000009 | Can not start ability in wukong mode. | +| 16000050 | Internal Error. | + **示例:** + 前台启动: + ```js - let caller = undefined; + var caller = undefined; // 后台启动Ability,不配置parameters var wantBackground = { @@ -754,13 +1386,29 @@ startAbilityByCall(want: Want): Promise<Caller>; abilityName: "MainAbility", deviceId: "" }; - this.context.startAbilityByCall(wantBackground) - .then((obj) => { + + try { + this.context.startAbilityByCall(wantBackground) + .then((obj) => { + // 执行正常业务 caller = obj; - console.log('GetCaller Success'); - }).catch((error) => { - console.log(`GetCaller failed with ${error}`); - }); + console.log('startAbilityByCall succeed'); + }).catch((error) => { + // 处理业务逻辑错误 + console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } + ``` + + 前台启动: + + ```js + var caller = undefined; // 前台启动Ability,将parameters中的"ohos.aafwk.param.callAbilityToForeground"配置为true var wantForeground = { @@ -772,12 +1420,22 @@ startAbilityByCall(want: Want): Promise<Caller>; "ohos.aafwk.param.callAbilityToForeground": true } }; - this.context.startAbilityByCall(wantForeground) - .then((obj) => { + + try { + this.context.startAbilityByCall(wantBackground) + .then((obj) => { + // 执行正常业务 caller = obj; - console.log('GetCaller success'); - }).catch((error) => { - console.log(`GetCaller failed with ${error}`); - }); + console.log('startAbilityByCall succeed'); + }).catch((error) => { + // 处理业务逻辑错误 + console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(err.code) + + ' error.message: ' + JSON.stringify(err.message)); + }); + } catch (paramError) { + // 处理入参错误异常 + console.log('error.code: ' + JSON.stringify(paramError.code) + + ' error.message: ' + JSON.stringify(paramError.message)); + } ``` \ No newline at end of file -- GitLab