提交 74984930 编写于 作者: H HuangXW

Context错误码整改

Signed-off-by: NHuangXW <huangxinwei4@huawei.com>
上级 0e327401
...@@ -40,7 +40,7 @@ Caller及Callee功能如下:具体的API详见[接口文档](../reference/apis ...@@ -40,7 +40,7 @@ Caller及Callee功能如下:具体的API详见[接口文档](../reference/apis
|call(method: string, data: rpc.Sequenceable): Promise\<void>|向通用组件Callee发送约定序列化数据。| |call(method: string, data: rpc.Sequenceable): Promise\<void>|向通用组件Callee发送约定序列化数据。|
|callWithResult(method: string, data: rpc.Sequenceable): Promise\<rpc.MessageParcel>|向通用组件Callee发送约定序列化数据, 并将Callee返回的约定序列化数据带回。| |callWithResult(method: string, data: rpc.Sequenceable): Promise\<rpc.MessageParcel>|向通用组件Callee发送约定序列化数据, 并将Callee返回的约定序列化数据带回。|
|release(): void|释放通用组件的Caller通信接口。| |release(): void|释放通用组件的Caller通信接口。|
|onRelease(callback: OnReleaseCallBack): void|注册通用组件通信断开监听通知。| |on(type: "release", callback: OnReleaseCallback): void|注册通用组件通信断开监听通知。|
## 开发步骤 ## 开发步骤
Call调用的开发步骤: Call调用的开发步骤:
...@@ -72,7 +72,7 @@ Ability配置标签示例如下: ...@@ -72,7 +72,7 @@ Ability配置标签示例如下:
``` ```
**2. 导入Ability模块** **2. 导入Ability模块**
```ts ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.app.ability.Ability'
``` ```
**3. 定义约定的序列化数据** **3. 定义约定的序列化数据**
...@@ -142,7 +142,7 @@ export default class CalleeAbility extends Ability { ...@@ -142,7 +142,7 @@ export default class CalleeAbility extends Ability {
### 访问Callee被调用端 ### 访问Callee被调用端
**1. 导入Ability模块** **1. 导入Ability模块**
```ts ```ts
import Ability from '@ohos.application.Ability' import Ability from '@ohos.app.ability.Ability'
``` ```
**2. 获取Caller通信接口** **2. 获取Caller通信接口**
...@@ -151,7 +151,7 @@ import Ability from '@ohos.application.Ability' ...@@ -151,7 +151,7 @@ import Ability from '@ohos.application.Ability'
// 注册caller的release监听 // 注册caller的release监听
private regOnRelease(caller) { private regOnRelease(caller) {
try { try {
caller.onRelease((msg) => { caller.on("release", (msg) => {
console.log(`caller onRelease is called ${msg}`) console.log(`caller onRelease is called ${msg}`)
}) })
console.log('caller register OnRelease succeed') console.log('caller register OnRelease succeed')
...@@ -192,7 +192,7 @@ async onButtonGetRemoteCaller() { ...@@ -192,7 +192,7 @@ async onButtonGetRemoteCaller() {
caller = data caller = data
console.log('get remote caller success') console.log('get remote caller success')
// 注册caller的release监听 // 注册caller的release监听
caller.onRelease((msg) => { caller.on("release", (msg) => {
console.log(`remote caller onRelease is called ${msg}`) console.log(`remote caller onRelease is called ${msg}`)
}) })
console.log('remote caller register OnRelease succeed') console.log('remote caller register OnRelease succeed')
......
...@@ -45,7 +45,7 @@ Ability创建时回调,执行初始化业务逻辑操作。 ...@@ -45,7 +45,7 @@ Ability创建时回调,执行初始化业务逻辑操作。
| param | AbilityConstant.LaunchParam | 是 | 创建&nbsp;ability、上次异常退出的原因信息。 | | param | AbilityConstant.LaunchParam | 是 | 创建&nbsp;ability、上次异常退出的原因信息。 |
**示例:** **示例:**
```js ```js
class myAbility extends Ability { class myAbility extends Ability {
onCreate(want, param) { onCreate(want, param) {
...@@ -320,7 +320,6 @@ onMemoryLevel(level: AbilityConstant.MemoryLevel): void; ...@@ -320,7 +320,6 @@ onMemoryLevel(level: AbilityConstant.MemoryLevel): void;
通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。 通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。
## Caller.call ## Caller.call
call(method: string, data: rpc.Sequenceable): Promise&lt;void&gt;; call(method: string, data: rpc.Sequenceable): Promise&lt;void&gt;;
...@@ -342,54 +341,67 @@ call(method: string, data: rpc.Sequenceable): Promise&lt;void&gt;; ...@@ -342,54 +341,67 @@ call(method: string, data: rpc.Sequenceable): Promise&lt;void&gt;;
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise形式返回应答。 | | Promise&lt;void&gt; | 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 ```js
import Ability from '@ohos.application.Ability'; import Ability from '@ohos.app.ability.Ability';
class MyMessageAble{ // 自定义的Sequenceable数据结构 class MyMessageAble{ // 自定义的Sequenceable数据结构
name:"" name:""
str:"" str:""
num: 1 num: 1
constructor(name, str) { constructor(name, str) {
this.name = name; this.name = name;
this.str = str; this.str = str;
} }
marshalling(messageParcel) { marshalling(messageParcel) {
messageParcel.writeInt(this.num); messageParcel.writeInt(this.num);
messageParcel.writeString(this.str); messageParcel.writeString(this.str);
console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
return true; return true;
} }
unmarshalling(messageParcel) { unmarshalling(messageParcel) {
this.num = messageParcel.readInt(); this.num = messageParcel.readInt();
this.str = messageParcel.readString(); this.str = messageParcel.readString();
console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
return true; return true;
} }
}; };
var method = 'call_Function'; // 约定的通知消息字符串 var method = 'call_Function'; // 约定的通知消息字符串
var caller; var caller;
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
this.context.startAbilityByCall({ this.context.startAbilityByCall({
bundleName: "com.example.myservice", bundleName: "com.example.myservice",
abilityName: "MainAbility", abilityName: "MainAbility",
deviceId: "" deviceId: ""
}).then((obj) => { }).then((obj) => {
caller = obj; caller = obj;
let msg = new MyMessageAble(1, "world"); // 参考Sequenceable数据定义 let msg = new MyMessageAble(1, "world"); // 参考Sequenceable数据定义
caller.call(method, msg) caller.call(method, msg)
.then(() => { .then(() => {
console.log('Caller call() called'); console.log('Caller call() called');
}).catch((e) => { })
console.log('Caller call() catch error ' + e); .catch((callErr) => {
}); console.log('Caller.call catch error, error.code: ' + JSON.stringify(callErr.code) +
console.log('Caller GetCaller Get ' + caller); ' error.message: ' + JSON.stringify(callErr.message));
}).catch((e) => { });
console.log('Caller GetCaller error ' + e); }).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&lt;rpc.MessagePa ...@@ -415,55 +427,69 @@ callWithResult(method: string, data: rpc.Sequenceable): Promise&lt;rpc.MessagePa
| -------- | -------- | | -------- | -------- |
| Promise&lt;rpc.MessageParcel&gt; | Promise形式返回通用组件服务端应答数据。 | | Promise&lt;rpc.MessageParcel&gt; | 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 ```js
import Ability from '@ohos.application.Ability'; import Ability from '@ohos.app.ability.Ability';
class MyMessageAble{ class MyMessageAble{
name:"" name:""
str:"" str:""
num: 1 num: 1
constructor(name, str) { constructor(name, str) {
this.name = name; this.name = name;
this.str = str; this.str = str;
} }
marshalling(messageParcel) { marshalling(messageParcel) {
messageParcel.writeInt(this.num); messageParcel.writeInt(this.num);
messageParcel.writeString(this.str); messageParcel.writeString(this.str);
console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
return true; return true;
} }
unmarshalling(messageParcel) { unmarshalling(messageParcel) {
this.num = messageParcel.readInt(); this.num = messageParcel.readInt();
this.str = messageParcel.readString(); this.str = messageParcel.readString();
console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
return true; return true;
} }
}; };
var method = 'call_Function'; var method = 'call_Function';
var caller; var caller;
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
this.context.startAbilityByCall({ this.context.startAbilityByCall({
bundleName: "com.example.myservice", bundleName: "com.example.myservice",
abilityName: "MainAbility", abilityName: "MainAbility",
deviceId: "" deviceId: ""
}).then((obj) => { }).then((obj) => {
caller = obj; caller = obj;
let msg = new MyMessageAble(1, "world"); let msg = new MyMessageAble(1, "world");
caller.callWithResult(method, msg) caller.callWithResult(method, msg)
.then((data) => { .then((data) => {
console.log('Caller callWithResult() called'); console.log('Caller callWithResult() called');
let retmsg = new MyMessageAble(0, ""); let retmsg = new MyMessageAble(0, "");
data.readSequenceable(retmsg); data.readSequenceable(retmsg);
}).catch((e) => { })
console.log('Caller callWithResult() catch error ' + e); .catch((callErr) => {
}); console.log('Caller.callWithResult catch error, error.code: ' + JSON.stringify(callErr.code) +
console.log('Caller GetCaller Get ' + caller); ' error.message: ' + JSON.stringify(callErr.message));
}).catch((e) => { });
console.log('Caller GetCaller error ' + e); }).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; ...@@ -476,36 +502,48 @@ release(): void;
**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore **系统能力**: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 ```js
import Ability from '@ohos.application.Ability'; import Ability from '@ohos.app.ability.Ability';
var caller; var caller;
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
this.context.startAbilityByCall({ this.context.startAbilityByCall({
bundleName: "com.example.myservice", bundleName: "com.example.myservice",
abilityName: "MainAbility", abilityName: "MainAbility",
deviceId: "" deviceId: ""
}).then((obj) => { }).then((obj) => {
caller = obj; caller = obj;
try { try {
caller.release(); caller.release();
} catch (e) { } catch (releaseErr) {
console.log('Caller Release error ' + e); console.log('Caller.release catch error, error.code: ' + JSON.stringify(releaseErr.code) +
} ' error.message: ' + JSON.stringify(releaseErr.message));
console.log('Caller GetCaller Get ' + caller); }
}).catch((e) => { }).catch((err) => {
console.log('Caller GetCaller error ' + e); 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(桩)断开监听通知。 注册通用组件服务端Stub(桩)断开监听通知。
...@@ -515,33 +553,45 @@ onRelease(callback: OnReleaseCallBack): void; ...@@ -515,33 +553,45 @@ onRelease(callback: OnReleaseCallBack): void;
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| type | string | 是 | 监听releaseCall事件,固定为'release'。 |
| callback | OnReleaseCallBack | 是 | 返回onRelease回调结果。 | | callback | OnReleaseCallBack | 是 | 返回onRelease回调结果。 |
**错误码:**
以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
**示例:** **示例:**
```js ```js
import Ability from '@ohos.application.Ability'; import Ability from '@ohos.app.ability.Ability';
var caller; var caller;
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage) {
this.context.startAbilityByCall({ this.context.startAbilityByCall({
bundleName: "com.example.myservice", bundleName: "com.example.myservice",
abilityName: "MainAbility", abilityName: "MainAbility",
deviceId: "" deviceId: ""
}).then((obj) => { }).then((obj) => {
caller = obj; caller = obj;
try { try {
caller.onRelease((str) => { caller.on("release", (str) => {
console.log(' Caller OnRelease CallBack is called ' + str); console.log(' Caller OnRelease CallBack is called ' + str);
}); });
} catch (e) { } catch (error) {
console.log('Caller Release error ' + e); console.log('Caller.on catch error, error.code: ' + JSON.stringify(error.code) +
} ' error.message: ' + JSON.stringify(error.message));
console.log('Caller GetCaller Get ' + caller); }
}).catch((e) => { }).catch((err) => {
console.log('Caller GetCaller error ' + e); 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; ...@@ -550,7 +600,6 @@ onRelease(callback: OnReleaseCallBack): void;
通用组件服务端注册和解除客户端caller通知送信的callback接口。 通用组件服务端注册和解除客户端caller通知送信的callback接口。
## Callee.on ## Callee.on
on(method: string, callback: CalleeCallBack): void; on(method: string, callback: CalleeCallBack): void;
...@@ -566,10 +615,20 @@ on(method: string, callback: CalleeCallBack): void; ...@@ -566,10 +615,20 @@ on(method: string, callback: CalleeCallBack): void;
| method | string | 是 | 与客户端约定的通知消息字符串。 | | method | string | 是 | 与客户端约定的通知消息字符串。 |
| callback | CalleeCallBack | 是 | 一个rpc.MessageParcel类型入参的js通知同步回调函数,&nbsp;回调函数至少要返回一个空的rpc.Sequenceable数据对象,&nbsp;其他视为函数执行错误。 | | callback | CalleeCallBack | 是 | 一个rpc.MessageParcel类型入参的js通知同步回调函数,&nbsp;回调函数至少要返回一个空的rpc.Sequenceable数据对象,&nbsp;其他视为函数执行错误。 |
**错误码:**
以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200004 | Method registered. The method has registered. |
| 16000050 | Internal Error. |
**示例:** **示例:**
```js ```js
import Ability from '@ohos.application.Ability'; import Ability from '@ohos.app.ability.Ability';
class MyMessageAble{ class MyMessageAble{
name:"" name:""
str:"" str:""
...@@ -599,14 +658,18 @@ on(method: string, callback: CalleeCallBack): void; ...@@ -599,14 +658,18 @@ on(method: string, callback: CalleeCallBack): void;
return new MyMessageAble(10, "Callee test"); return new MyMessageAble(10, "Callee test");
} }
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onCreate(want, launchParam) { onCreate(want, launchParam) {
console.log('Callee onCreate is called'); console.log('Callee onCreate is called');
this.callee.on(method, funcCallBack); 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 ## Callee.off
off(method: string): void; off(method: string): void;
...@@ -621,20 +684,36 @@ off(method: string): void; ...@@ -621,20 +684,36 @@ off(method: string): void;
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| method | string | 是 | 已注册的通知事件字符串。 | | method | string | 是 | 已注册的通知事件字符串。 |
**错误码:**
以下错误码的详细介绍请参见[元能力错误码](../errorcodes/errorcode-ability.md)
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200005 | Method not registered. The method has not registered. |
| 16000050 | Internal Error. |
**示例:** **示例:**
```js ```js
import Ability from '@ohos.application.Ability'; import Ability from '@ohos.app.ability.Ability';
var method = 'call_Function'; var method = 'call_Function';
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onCreate(want, launchParam) { onCreate(want, launchParam) {
console.log('Callee onCreate is called'); console.log('Callee onCreate is called');
this.callee.off(method); 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; (msg: string): void;
...@@ -644,7 +723,7 @@ off(method: string): void; ...@@ -644,7 +723,7 @@ off(method: string): void;
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| (msg: string) | function | 是 | 否 | 调用者注册的侦听器函数接口的原型。 | | (msg: string) | function | 是 | 否 | 调用者注册的侦听器函数接口的原型。 |
## CalleeCallBack ## CalleeCallback
(indata: rpc.MessageParcel): rpc.Sequenceable; (indata: rpc.MessageParcel): rpc.Sequenceable;
......
...@@ -31,7 +31,7 @@ ExtensionContext主要用于查询所属Extension的信息、Module的配置信 ...@@ -31,7 +31,7 @@ ExtensionContext主要用于查询所属Extension的信息、Module的配置信
三个Module内都定义一个相同名称的ServiceExtension: 三个Module内都定义一个相同名称的ServiceExtension:
``` js ``` js
import ServiceExtension from '@ohos.application.ServiceExtensionAbility' import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'
import Want from '@ohos.application.Want' import Want from '@ohos.application.Want'
export default class TheServiceExtension extends ServiceExtension { export default class TheServiceExtension extends ServiceExtension {
onCreate(want:Want) { onCreate(want:Want) {
...@@ -61,7 +61,7 @@ export default class TheServiceExtension extends ServiceExtension { ...@@ -61,7 +61,7 @@ export default class TheServiceExtension extends ServiceExtension {
在entry的MainAbility的onCreate回调内启动ServiceExtension 在entry的MainAbility的onCreate回调内启动ServiceExtension
``` js ``` js
import Ability from '@ohos.application.Ability' import Ability from '@ohos.app.ability.Ability'
export default class MainAbility extends Ability { export default class MainAbility extends Ability {
onCreate(want, launchParam) { onCreate(want, launchParam) {
console.log("[Demo] MainAbility onCreate"); console.log("[Demo] MainAbility onCreate");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册