鸿蒙原生应用/元服务开发-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官方开发文档整理

相关推荐
dawn5 小时前
鸿蒙ArkTS中的获取网络数据
华为·harmonyos
桃花键神5 小时前
鸿蒙5.0时代:原生鸿蒙应用市场引领开发者服务新篇章
华为·harmonyos
鸿蒙自习室5 小时前
鸿蒙多线程开发——并发模型对比(Actor与内存共享)
华为·harmonyos
JavaPub-rodert6 小时前
鸿蒙生态崛起:开发者的机遇与挑战
华为·harmonyos
帅比九日8 小时前
【HarmonyOS Next】封装一个网络请求模块
前端·harmonyos
yilylong10 小时前
鸿蒙(Harmony)实现滑块验证码
华为·harmonyos·鸿蒙
baby_hua10 小时前
HarmonyOS第一课——DevEco Studio的使用
华为·harmonyos
HarmonyOS_SDK10 小时前
融合虚拟与现实,AR Engine为用户提供沉浸式交互体验
harmonyos
- 羊羊不超越 -11 小时前
App渠道来源追踪方案全面分析(iOS/Android/鸿蒙)
android·ios·harmonyos
长弓三石13 小时前
鸿蒙网络编程系列44-仓颉版HttpRequest上传文件示例
前端·网络·华为·harmonyos·鸿蒙