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

@ohos.app.ability.UIAbility (UIAbility)
Caller

通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。

Caller.call

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

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

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

示例:

class MyMessageAble{ // 自定义的Parcelable数据结构
  name:''
  str:''
  num: 1
  constructor(name, str) {
    this.name = name;
    this.str = str;
  }
  marshalling(messageSequence) {
    messageSequence.writeInt(this.num);
    messageSequence.writeString(this.str);
    console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
    return true;
  }
  unmarshalling(messageSequence) {
    this.num = messageSequence.readInt();
    this.str = messageSequence.readString();
    console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
    return true;
  }
};
let method = 'call_Function'; // 约定的通知消息字符串
let 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('msg', 'world'); // 参考Parcelable数据定义
      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));
    });
  }
}

Caller.callWithResult

callWithResult(method: string, data: rpc.Parcelable): Promise<rpc.MessageSequence>;

向通用组件服务端发送约定序列化数据, 并将服务端返回的约定序列化数据带回。

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

示例:

class MyMessageAble{
  name:''
  str:''
  num: 1
  constructor(name, str) {
    this.name = name;
    this.str = str;
  }
  marshalling(messageSequence) {
    messageSequence.writeInt(this.num);
    messageSequence.writeString(this.str);
    console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
    return true;
  }
  unmarshalling(messageSequence) {
    this.num = messageSequence.readInt();
    this.str = messageSequence.readString();
    console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
    return true;
  }
};
let method = 'call_Function';
let 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.readParcelable(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));
    });
  }
}

Caller.release

release(): void;

主动释放通用组件服务端的通信接口。

系统能力:SystemCapability.UIAbility.UIAbilityRuntime.UIAbilityCore

错误码:

示例

let 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 (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

onRelease(callback: OnReleaseCallback): void;

注册通用组件服务端Stub(桩)断开监听通知。

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

示例;

let 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 (error) {
          console.log('Caller.onRelease 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));
    });
  }
}

Caller.on

on(type: 'release', callback: OnReleaseCallback): void;

注册通用组件服务端Stub(桩)断开监听通知。

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

示例:

let 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.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));
    });
  }
}

Caller.off

off(type: 'release', callback: OnReleaseCallback): void;

取消注册通用组件服务端Stub(桩)断开监听通知。预留能力,当前暂未支持。

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

示例:

let caller;
export default class MainUIAbility extends Ability {
  onWindowStageCreate(windowStage) {
    this.context.startAbilityByCall({
      bundleName: 'com.example.myservice',
      abilityName: 'MainUIAbility',
      deviceId: ''
    }).then((obj) => {
        caller = obj;
        try {
          let onReleaseCallBack = (str) => {
              console.log(' Caller OnRelease CallBack is called ' + str);
          };
          caller.on('release', onReleaseCallBack);
          caller.off('release', onReleaseCallBack);
        } catch (error) {
          console.log('Caller.on or Caller.off 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));
    });
  }
}

Caller.off

off(type: 'release'): void;

取消注册通用组件服务端Stub(桩)断开监听通知。预留能力,当前暂未支持。

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

示例:

let caller;
export default class MainUIAbility extends Ability {
  onWindowStageCreate(windowStage) {
    this.context.startAbilityByCall({
      bundleName: 'com.example.myservice',
      abilityName: 'MainUIAbility',
      deviceId: ''
    }).then((obj) => {
        caller = obj;
        try {
          let onReleaseCallBack = (str) => {
              console.log(' Caller OnRelease CallBack is called ' + str);
          };
          caller.on('release', onReleaseCallBack);
          caller.off('release');
        } catch (error) {  
          console.error('Caller.on or Caller.off catch error, error.code: ' + JSON.stringify(error.code) +
            ' error.message: ' + JSON.stringify(error.message));
        }
    }).catch((err) => {
      console.error('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
        ' error.message: ' + JSON.stringify(err.message));
    });
  }
}

Callee

通用组件服务端注册和解除客户端caller通知送信的callback接口。
Callee.on

on(method: string, callback: CalleeCallback): void;

通用组件服务端注册消息通知callback。

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

示例:

class MyMessageAble{
    name:''
    str:''
    num: 1
    constructor(name, str) {
      this.name = name;
      this.str = str;
    }
    marshalling(messageSequence) {
        messageSequence.writeInt(this.num);
        messageSequence.writeString(this.str);
        console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']');
        return true;
    }
    unmarshalling(messageSequence) {
        this.num = messageSequence.readInt();
        this.str = messageSequence.readString();
        console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']');
        return true;
    }
};
let method = 'call_Function';
function funcCallBack(pdata) {
    console.log('Callee funcCallBack is called ' + pdata);
    let msg = new MyMessageAble('test', '');
    pdata.readParcelable(msg);
    return new MyMessageAble('test1', 'Callee test');
}
export default class MainAbility extends Ability {
  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;

解除通用组件服务端注册消息通知callback。

系统能力:SystemCapability.UIAbility.UIAbilityRuntime.UIAbilityCore

示例:

let method = 'call_Function';
export default class MainAbility extends Ability {
  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

(msg: string): void;

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

相关推荐
Random_index7 小时前
#Uniapp篇:支持纯血鸿蒙&发布&适配&UIUI
uni-app·harmonyos
鸿蒙自习室11 小时前
鸿蒙多线程开发——线程间数据通信对象02
ui·harmonyos·鸿蒙
SuperHeroWu713 小时前
【HarmonyOS】鸿蒙应用接入微博分享
华为·harmonyos·鸿蒙·微博·微博分享·微博sdk集成·sdk集成
期待未来的男孩13 小时前
华为FusionCube 500-8.2.0SPC100 实施部署文档
华为
岳不谢15 小时前
VPN技术-VPN简介学习笔记
网络·笔记·学习·华为
zhangjr057515 小时前
【HarmonyOS Next】鸿蒙实用装饰器一览(一)
前端·harmonyos·arkts
诗歌难吟4641 天前
初识ArkUI
harmonyos
SameX1 天前
HarmonyOS Next 设备安全特性深度剖析学习
harmonyos
郭梧悠1 天前
HarmonyOS(57) UI性能优化
ui·性能优化·harmonyos
郝晨妤1 天前
鸿蒙原生应用开发元服务 元服务是什么?和App的关系?(保姆级步骤)
android·ios·华为od·华为·华为云·harmonyos·鸿蒙