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

2023-12-22 16:08:25

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

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

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

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

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


?

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


示例:

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
?

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


?

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


示例:

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
错误码:
?

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


示例

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

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


示例;

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
?

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


示例:

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
?

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


示例:

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
?

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


示例:

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
?

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


示例:

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
?

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


示例:

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;

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

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

文章来源:https://blog.csdn.net/weixin_69135651/article/details/135153193
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。