当前位置:   article > 正文

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下_messagesequence.writestringarray()

messagesequence.writestringarray()

@ohos.app.ability.UIAbility (UIAbility)
Caller
通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。

Caller.call
call(method: string, data: rpc.Parcelable): Promise<void>;

向通用组件服务端发送约定序列化数据。

系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. class MyMessageAble{ // 自定义的Parcelable数据结构
  2. name:''
  3. str:''
  4. num: 1
  5. constructor(name, str) {
  6. this.name = name;
  7. this.str = str;
  8. }
  9. marshalling(messageSequence) {
  10. messageSequence.writeInt(this.num);
  11. messageSequence.writeString(this.str);
  12. console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
  13. return true;
  14. }
  15. unmarshalling(messageSequence) {
  16. this.num = messageSequence.readInt();
  17. this.str = messageSequence.readString();
  18. console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
  19. return true;
  20. }
  21. };
  22. let method = 'call_Function'; // 约定的通知消息字符串
  23. let caller;
  24. export default class MainAbility extends Ability {
  25. onWindowStageCreate(windowStage) {
  26. this.context.startAbilityByCall({
  27. bundleName: 'com.example.myservice',
  28. abilityName: 'MainAbility',
  29. deviceId: ''
  30. }).then((obj) => {
  31. caller = obj;
  32. let msg = new MyMessageAble('msg', 'world'); // 参考Parcelable数据定义
  33. caller.call(method, msg)
  34. .then(() => {
  35. console.log('Caller call() called');
  36. })
  37. .catch((callErr) => {
  38. console.log('Caller.call catch error, error.code: ' + JSON.stringify(callErr.code) +
  39. ' error.message: ' + JSON.stringify(callErr.message));
  40. });
  41. }).catch((err) => {
  42. console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  43. ' error.message: ' + JSON.stringify(err.message));
  44. });
  45. }
  46. }

Caller.callWithResult
callWithResult(method: string, data: rpc.Parcelable): Promise<rpc.MessageSequence>;
向通用组件服务端发送约定序列化数据, 并将服务端返回的约定序列化数据带回。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. class MyMessageAble{
  2. name:''
  3. str:''
  4. num: 1
  5. constructor(name, str) {
  6. this.name = name;
  7. this.str = str;
  8. }
  9. marshalling(messageSequence) {
  10. messageSequence.writeInt(this.num);
  11. messageSequence.writeString(this.str);
  12. console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
  13. return true;
  14. }
  15. unmarshalling(messageSequence) {
  16. this.num = messageSequence.readInt();
  17. this.str = messageSequence.readString();
  18. console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
  19. return true;
  20. }
  21. };
  22. let method = 'call_Function';
  23. let caller;
  24. export default class MainAbility extends Ability {
  25. onWindowStageCreate(windowStage) {
  26. this.context.startAbilityByCall({
  27. bundleName: 'com.example.myservice',
  28. abilityName: 'MainAbility',
  29. deviceId: ''
  30. }).then((obj) => {
  31. caller = obj;
  32. let msg = new MyMessageAble(1, 'world');
  33. caller.callWithResult(method, msg)
  34. .then((data) => {
  35. console.log('Caller callWithResult() called');
  36. let retmsg = new MyMessageAble(0, '');
  37. data.readParcelable(retmsg);
  38. })
  39. .catch((callErr) => {
  40. console.log('Caller.callWithResult catch error, error.code: ' + JSON.stringify(callErr.code) +
  41. ' error.message: ' + JSON.stringify(callErr.message));
  42. });
  43. }).catch((err) => {
  44. console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  45. ' error.message: ' + JSON.stringify(err.message));
  46. });
  47. }
  48. }

Caller.release
release(): void;
主动释放通用组件服务端的通信接口。
系统能力:SystemCapability.UIAbility.UIAbilityRuntime.UIAbilityCore
错误码:
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例

  1. let caller;
  2. export default class MainAbility extends Ability {
  3. onWindowStageCreate(windowStage) {
  4. this.context.startAbilityByCall({
  5. bundleName: 'com.example.myservice',
  6. abilityName: 'MainAbility',
  7. deviceId: ''
  8. }).then((obj) => {
  9. caller = obj;
  10. try {
  11. caller.release();
  12. } catch (releaseErr) {
  13. console.log('Caller.release catch error, error.code: ' + JSON.stringify(releaseErr.code) +
  14. ' error.message: ' + JSON.stringify(releaseErr.message));
  15. }
  16. }).catch((err) => {
  17. console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  18. ' error.message: ' + JSON.stringify(err.message));
  19. });
  20. }
  21. }

Caller.onRelease
onRelease(callback: OnReleaseCallback): void;
注册通用组件服务端Stub(桩)断开监听通知。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例;

  1. let caller;
  2. export default class MainAbility extends Ability {
  3. onWindowStageCreate(windowStage) {
  4. this.context.startAbilityByCall({
  5. bundleName: 'com.example.myservice',
  6. abilityName: 'MainAbility',
  7. deviceId: ''
  8. }).then((obj) => {
  9. caller = obj;
  10. try {
  11. caller.onRelease((str) => {
  12. console.log(' Caller OnRelease CallBack is called ' + str);
  13. });
  14. } catch (error) {
  15. console.log('Caller.onRelease catch error, error.code: ' + JSON.stringify(error.code) +
  16. ' error.message: ' + JSON.stringify(error.message));
  17. }
  18. }).catch((err) => {
  19. console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  20. ' error.message: ' + JSON.stringify(err.message));
  21. });
  22. }
  23. }

Caller.on
on(type: ‘release’, callback: OnReleaseCallback): void;
注册通用组件服务端Stub(桩)断开监听通知。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. let caller;
  2. export default class MainAbility extends Ability {
  3. onWindowStageCreate(windowStage) {
  4. this.context.startAbilityByCall({
  5. bundleName: 'com.example.myservice',
  6. abilityName: 'MainAbility',
  7. deviceId: ''
  8. }).then((obj) => {
  9. caller = obj;
  10. try {
  11. caller.on('release', (str) => {
  12. console.log(' Caller OnRelease CallBack is called ' + str);
  13. });
  14. } catch (error) {
  15. console.log('Caller.on catch error, error.code: ' + JSON.stringify(error.code) +
  16. ' error.message: ' + JSON.stringify(error.message));
  17. }
  18. }).catch((err) => {
  19. console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  20. ' error.message: ' + JSON.stringify(err.message));
  21. });
  22. }
  23. }

Caller.off
off(type: ‘release’, callback: OnReleaseCallback): void;
取消注册通用组件服务端Stub(桩)断开监听通知。预留能力,当前暂未支持。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. let caller;
  2. export default class MainUIAbility extends Ability {
  3. onWindowStageCreate(windowStage) {
  4. this.context.startAbilityByCall({
  5. bundleName: 'com.example.myservice',
  6. abilityName: 'MainUIAbility',
  7. deviceId: ''
  8. }).then((obj) => {
  9. caller = obj;
  10. try {
  11. let onReleaseCallBack = (str) => {
  12. console.log(' Caller OnRelease CallBack is called ' + str);
  13. };
  14. caller.on('release', onReleaseCallBack);
  15. caller.off('release', onReleaseCallBack);
  16. } catch (error) {
  17. console.log('Caller.on or Caller.off catch error, error.code: ' + JSON.stringify(error.code) +
  18. ' error.message: ' + JSON.stringify(error.message));
  19. }
  20. }).catch((err) => {
  21. console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  22. ' error.message: ' + JSON.stringify(err.message));
  23. });
  24. }
  25. }

Caller.off
off(type: ‘release’): void;
取消注册通用组件服务端Stub(桩)断开监听通知。预留能力,当前暂未支持。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. let caller;
  2. export default class MainUIAbility extends Ability {
  3. onWindowStageCreate(windowStage) {
  4. this.context.startAbilityByCall({
  5. bundleName: 'com.example.myservice',
  6. abilityName: 'MainUIAbility',
  7. deviceId: ''
  8. }).then((obj) => {
  9. caller = obj;
  10. try {
  11. let onReleaseCallBack = (str) => {
  12. console.log(' Caller OnRelease CallBack is called ' + str);
  13. };
  14. caller.on('release', onReleaseCallBack);
  15. caller.off('release');
  16. } catch (error) {
  17. console.error('Caller.on or Caller.off catch error, error.code: ' + JSON.stringify(error.code) +
  18. ' error.message: ' + JSON.stringify(error.message));
  19. }
  20. }).catch((err) => {
  21. console.error('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
  22. ' error.message: ' + JSON.stringify(err.message));
  23. });
  24. }
  25. }

Callee
通用组件服务端注册和解除客户端caller通知送信的callback接口。
Callee.on
on(method: string, callback: CalleeCallback): void;
通用组件服务端注册消息通知callback。
系统能力:SystemCapability.Ability.AbilityRuntime.AbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. class MyMessageAble{
  2. name:''
  3. str:''
  4. num: 1
  5. constructor(name, str) {
  6. this.name = name;
  7. this.str = str;
  8. }
  9. marshalling(messageSequence) {
  10. messageSequence.writeInt(this.num);
  11. messageSequence.writeString(this.str);
  12. console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
  13. return true;
  14. }
  15. unmarshalling(messageSequence) {
  16. this.num = messageSequence.readInt();
  17. this.str = messageSequence.readString();
  18. console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
  19. return true;
  20. }
  21. };
  22. let method = 'call_Function';
  23. function funcCallBack(pdata) {
  24. console.log('Callee funcCallBack is called ' + pdata);
  25. let msg = new MyMessageAble('test', '');
  26. pdata.readParcelable(msg);
  27. return new MyMessageAble('test1', 'Callee test');
  28. }
  29. export default class MainAbility extends Ability {
  30. onCreate(want, launchParam) {
  31. console.log('Callee onCreate is called');
  32. try {
  33. this.callee.on(method, funcCallBack);
  34. } catch (error) {
  35. console.log('Callee.on catch error, error.code: ' + JSON.stringify(error.code) +
  36. ' error.message: ' + JSON.stringify(error.message));
  37. }
  38. }
  39. }

Callee.off
off(method: string): void;
解除通用组件服务端注册消息通知callback。
系统能力:SystemCapability.UIAbility.UIAbilityRuntime.UIAbilityCore
 

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区


示例:

  1. let method = 'call_Function';
  2. export default class MainAbility extends Ability {
  3. onCreate(want, launchParam) {
  4. console.log('Callee onCreate is called');
  5. try {
  6. this.callee.off(method);
  7. } catch (error) {
  8. console.log('Callee.off catch error, error.code: ' + JSON.stringify(error.code) +
  9. ' error.message: ' + JSON.stringify(error.message));
  10. }
  11. }
  12. }

OnReleaseCallback
(msg: string): void;

鸿蒙原生应用/元服务开发-Stage模型能力接口(九)下-鸿蒙开发者社区

本文根据HarmonyOS官方开发文档整理

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/322807
推荐阅读
相关标签
  

闽ICP备14008679号