harmony 鸿蒙@ohos.app.ability.UIAbility (UIAbility)

  • 2022-12-13
  • 浏览 (1092)

@ohos.app.ability.UIAbility (UIAbility)

UIAbility是包含UI界面的应用组件,提供组件创建、销毁、前后台切换等生命周期回调,同时也具备组件协同的能力,组件协同主要提供如下常用功能:

  • Caller:由startAbilityByCall接口返回,CallerAbility(调用者)可使用Caller与CalleeAbility(被调用者)进行通信。
  • Callee:UIAbility的内部对象,CalleeAbility(被调用者)可以通过Callee与Caller进行通信。

说明:

本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 本模块接口仅可在Stage模型下使用。

导入模块

import UIAbility from '@ohos.app.ability.UIAbility';

属性

系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.AbilityCore

名称 类型 可读 可写 说明
context UIAbilityContext 上下文。
launchWant Want UIAbility启动时的参数。
lastRequestWant Want UIAbility最后请求时的参数。
callee Callee 调用Stub(桩)服务对象。

UIAbility.onCreate

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void;

UIAbility实例处于完全关闭状态下被创建完成后进入该生命周期回调,执行初始化业务逻辑操作。即UIAbility实例冷启动时进入该生命周期回调。

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

参数:

参数名 类型 必填 说明
want Want 当前UIAbility的Want类型信息,包括ability名称、bundle名称等。
param AbilityConstant.LaunchParam 创建 ability、上次异常退出的原因信息。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  import Want from '@ohos.app.ability.Want';

  class MyUIAbility extends UIAbility {
      onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
          console.log('onCreate, want: ${want.abilityName}');
      }
  }

UIAbility.onWindowStageCreate

onWindowStageCreate(windowStage: window.WindowStage): void

当WindowStage创建后调用。

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

参数:

参数名 类型 必填 说明
windowStage window.WindowStage WindowStage相关信息。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import window from '@ohos.window';

  class MyUIAbility extends UIAbility {
      onWindowStageCreate(windowStage: window.WindowStage) {
          console.log('onWindowStageCreate');
      }
  }

UIAbility.onWindowStageDestroy

onWindowStageDestroy(): void

当WindowStage销毁后调用。

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

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';

  class MyUIAbility extends UIAbility {
      onWindowStageDestroy() {
          console.log('onWindowStageDestroy');
      }
  }

UIAbility.onWindowStageRestore

onWindowStageRestore(windowStage: window.WindowStage): void

当迁移多实例ability时,恢复WindowStage后调用。

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

参数:

参数名 类型 必填 说明
windowStage window.WindowStage WindowStage相关信息。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import window from '@ohos.window';

  class MyUIAbility extends UIAbility {
      onWindowStageRestore(windowStage: window.WindowStage) {
          console.log('onWindowStageRestore');
      }
  }

UIAbility.onDestroy

onDestroy(): void|Promise<void>;

UIAbility生命周期回调,在销毁时回调,执行资源清理等操作。

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

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';

  class MyUIAbility extends UIAbility {
      onDestroy() {
          console.log('onDestroy');
      }
  }

在执行完onDestroy生命周期回调后,应用可能会退出,从而可能导致onDestroy中的异步函数未能正确执行,比如异步写入数据库。可以使用异步生命周期,以确保异步onDestroy完成后再继续后续的生命周期。

import UIAbility from '@ohos.app.ability.UIAbility';

class MyUIAbility extends UIAbility {
    async onDestroy() {
        console.log('onDestroy');
        // 调用异步函数...
    }
}

UIAbility.onForeground

onForeground(): void;

UIAbility生命周期回调,当应用从后台转到前台时触发。

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

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';

  class MyUIAbility extends UIAbility {
      onForeground() {
          console.log('onForeground');
      }
  }

UIAbility.onBackground

onBackground(): void;

UIAbility生命周期回调,当应用从前台转到后台时触发。

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

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';

  class MyUIAbility extends UIAbility {
      onBackground() {
          console.log('onBackground');
      }
  }

UIAbility.onContinue

onContinue(wantParam: { [key: string]: Object }): AbilityConstant.OnContinueResult;

当ability迁移准备迁移时触发,保存数据。

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

参数:

参数名 类型 必填 说明
wantParam {[key: string]: Object} want相关参数。

返回值:

类型 说明
AbilityConstant.OnContinueResult 继续的结果。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';

  class MyUIAbility extends UIAbility {
      onContinue(wantParams: Record<string, Object>) {
          console.log('onContinue');
          wantParams['myData'] = 'my1234567';
          return AbilityConstant.OnContinueResult.AGREE;
      }
  }

UIAbility.onNewWant

onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void;

UIAbility实例已经启动并在前台运行过,由于某些原因切换到后台,再次启动该UIAbility实例时会回调执行该方法。即UIAbility实例热启动时进入该生命周期回调。

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

参数:

参数名 类型 必填 说明
want Want Want类型参数,如ability名称,包名等。
launchParam AbilityConstant.LaunchParam UIAbility启动的原因、上次异常退出的原因信息。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  import Want from '@ohos.app.ability.Want';

  class MyUIAbility extends UIAbility {
      onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) {
          console.log(`onNewWant, want: ${want.abilityName}`);
          console.log(`onNewWant, launchParam: ${JSON.stringify(launchParam)}`);
      }
  }

UIAbility.onDump

onDump(params: Array<string>): Array<string>;

转储客户端信息时调用。

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

参数:

参数名 类型 必填 说明
params Array<string> 表示命令形式的参数。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';

  class MyUIAbility extends UIAbility {
      onDump(params: Array<string>) {
          console.log(`dump, params: ${JSON.stringify(params)}`);
          return ['params'];
      }
  }

UIAbility.onSaveState

onSaveState(reason: AbilityConstant.StateType, wantParam : {[key: string]: Object}): AbilityConstant.OnSaveResult;

该API配合appRecovery使用。在应用故障时,如果使能了自动保存状态,框架将回调onSaveState保存UIAbility状态。

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

参数:

参数名 类型 必填 说明
reason AbilityConstant.StateType 回调保存状态的原因。
wantParam {[key:&nbsp;string]:&nbsp;Object} want相关参数。

返回值:

类型 说明
AbilityConstant.OnSaveResult 是否同意保存当前UIAbility的状态。

示例:

import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';

class MyUIAbility extends UIAbility {
    onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
        console.log('onSaveState');
        wantParam['myData'] = 'my1234567';
        return AbilityConstant.OnSaveResult.RECOVERY_AGREE;
    }
}

UIAbility.onShare10+

onShare(wantParam:{ [key: string]: Object }): void;

在跨端分享场景下,在UIAbility中设置分享方设备要分享的数据。

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

参数:

参数名 类型 必填 说明
wantParam {[key:&nbsp;string]:&nbsp;Object} 待分享的数据。

示例:

import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';

class MyUIAbility extends UIAbility {
    onShare(wantParams: Record<string, Object>) {
        console.log('onShare');
        wantParams['ohos.extra.param.key.shareUrl'] = 'example.com';
    }
}

UIAbility.onPrepareToTerminate10+

onPrepareToTerminate(): boolean;

UIAbility生命周期回调,当系统预关闭开关打开后(配置系统参数persist.sys.prepare_terminate为true打开),在UIAbility关闭时触发,可在回调中定义操作来决定是否继续执行关闭UIAbility的操作。如果UIAbility在退出时需要与用户交互确认是否关闭UIAbility,可在此生命周期回调中定义预关闭操作配合terminateSelf接口退出,如弹窗确认是否关闭,并配置预关闭生命周期返回true取消正常关闭。

需要权限:ohos.permission.PREPARE_APP_TERMINATE

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

返回值:

|类型|说明| |–|–| |boolean|是否执行UIAbility关闭操作,返回true表示本次UIAbility关闭流程取消,不再退出,返回false表示UIAbility继续正常关闭。|

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  export default class EntryAbility extends UIAbility {
    onPrepareToTerminate() {
      // 开发者定义预关闭动作
      // 例如拉起另一个ability,根据ability处理结果执行异步关闭
      let want: Want = {
        bundleName: "com.example.myapplication",
        moduleName: "entry",
        abilityName: "SecondAbility"
      }
      this.context.startAbilityForResult(want)
        .then((result)=>{
          // 获取ability处理结果,当返回结果的resultCode为0关闭当前UIAbility
          console.log('startAbilityForResult success, resultCode is ' + result.resultCode);
          if (result.resultCode === 0) {
            this.context.terminateSelf();
          }
        }).catch((err: BusinessError)=>{
          // 异常处理
          console.log('startAbilityForResult failed, err:' + JSON.stringify(err));
          this.context.terminateSelf();
        })

      return true; // 已定义预关闭操作后,返回true表示UIAbility取消关闭
    }
  }

UIAbility.onBackPressed10+

onBackPressed(): boolean;

UIAbility生命周期回调,当UIAbility侧滑返回时触发。根据返回值决定是否销毁UIAbility,默认为销毁UIAbility。

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

返回值:

|类型|说明| |–|–| |boolean|返回true表示UIAbility将会被移到后台不销毁,返回false表示UIAbility将正常销毁。|

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';

  export default class EntryAbility extends UIAbility {
    onBackPressed() {
      return true;
    }
  }

Caller

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

Caller.call

call(method: string, data: rpc.Parcelable): Promise&lt;void&gt;;

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

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

参数:

参数名 类型 必填 说明
method string 约定的服务端注册事件字符串。
data rpc.Parcelable 由开发者实现的Parcelable可序列化数据。

返回值:

类型 说明
Promise&lt;void&gt; Promise形式返回应答。

错误码:

错误码ID 错误信息
16200001 Caller released. The caller has been released.
16200002 Callee invalid. The callee does not exist.
16000050 Internal error.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';
  import rpc from '@ohos.rpc';

  class MyMessageAble implements rpc.Parcelable { // 自定义的Parcelable数据结构
    name: string
    str: string
    num: number = 1
    constructor(name: string, str: string) {
      this.name = name;
      this.str = str;
    }
    marshalling(messageSequence: rpc.MessageSequence) {
      messageSequence.writeInt(this.num);
      messageSequence.writeString(this.str);
      console.log('MyMessageAble marshalling num[${this.num}] str[${this.str}]');
      return true;
    }
    unmarshalling(messageSequence: rpc.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: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        deviceId: ''
      }).then((obj) => {
        caller = obj;
        let msg = new MyMessageAble('msg', 'world'); // 参考Parcelable数据定义
        caller.call(method, msg)
          .then(() => {
            console.log('Caller call() called');
          })
          .catch((callErr: BusinessError) => {
            console.log('Caller.call catch error, error.code: ${callErr.code}, error.message: ${callErr.message}');
          });
      }).catch((err: BusinessError) => {
        console.log('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Caller.callWithResult

callWithResult(method: string, data: rpc.Parcelable): Promise&lt;rpc.MessageSequence&gt;;

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

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

参数:

参数名 类型 必填 说明
method string 约定的服务端注册事件字符串。
data rpc.Parcelable 由开发者实现的Parcelable可序列化数据。

返回值:

类型 说明
Promise&lt;rpc.MessageSequence&gt; Promise形式返回通用组件服务端应答数据。

错误码:

错误码ID 错误信息
16200001 Caller released. The caller has been released.
16200002 Callee invalid. The callee does not exist.
16000050 Internal error.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';
  import rpc from '@ohos.rpc';

  class MyMessageAble implements rpc.Parcelable {
    name: string
    str: string
    num: number = 1
    constructor(name: string, str: string) {
      this.name = name;
      this.str = str;
    }
    marshalling(messageSequence: rpc.MessageSequence) {
      messageSequence.writeInt(this.num);
      messageSequence.writeString(this.str);
      console.log('MyMessageAble marshalling num[${this.num}] str[${this.str}]');
      return true;
    }
    unmarshalling(messageSequence: rpc.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: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        deviceId: ''
      }).then((obj) => {
        caller = obj;
        let msg = new MyMessageAble('msg', 'world');
        caller.callWithResult(method, msg)
          .then((data) => {
            console.log('Caller callWithResult() called');
            let retmsg = new MyMessageAble('msg', 'world');
            data.readParcelable(retmsg);
          })
          .catch((callErr: BusinessError) => {
            console.log('Caller.callWithResult catch error, error.code: ${callErr.code}, error.message: ${callErr.message}');
          });
      }).catch((err: BusinessError) => {
        console.log('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Caller.release

release(): void;

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

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

错误码:

错误码ID 错误信息
16200001 Caller released. The caller has been released.
16200002 Callee invalid. The callee does not exist.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';

  let caller: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        deviceId: ''
      }).then((obj) => {
        caller = obj;
        try {
          caller.release();
        } catch (releaseErr) {
          console.log('Caller.release catch error, error.code: ${releaseErr.code}, error.message: ${releaseErr.message}');
        }
      }).catch((err: BusinessError) => {
        console.log('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Caller.onRelease

onRelease(callback: OnReleaseCallback): void;

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

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

错误码:

错误码ID 错误信息
16200001 Caller released. The caller has been released.

以上错误码详细介绍请参考errcode-ability

参数:

参数名 类型 必填 说明
callback OnReleaseCallback 返回onRelease回调结果。

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';

  let caller: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        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: $error.code}, error.message: ${error.message}');
          }
      }).catch((err: BusinessError) => {
        console.log('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Caller.onRemoteStateChange10+

onRemoteStateChange(callback: OnRemoteStateChangeCallback): void;

注册协同场景下跨设备组件状态变化监听通知。

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

参数:

参数名 类型 必填 说明
callback OnRemoteStateChangeCallback 返回onRemoteStateChange回调结果。

错误码:

错误码ID 错误信息
16200001 Caller released. The caller has been released.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';

  let caller: Caller;
  let dstDeviceId: string;
  export default class MainAbility extends UIAbility {
      onWindowStageCreate(windowStage: window.WindowStage) {
          this.context.startAbilityByCall({
              bundleName: 'com.example.myservice',
              abilityName: 'MainUIAbility',
              deviceId: dstDeviceId
          }).then((obj) => {
              caller = obj;
              try {
                  caller.onRemoteStateChange((str) => {
                      console.log('Remote state changed ' + str);
                  });
              } catch (error) {
                  console.log(`Caller.onRemoteStateChange catch error, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}`);
              }
          }).catch((err: BusinessError) => {
              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

参数:

参数名 类型 必填 说明
type string 监听releaseCall事件,固定为’release’。
callback OnReleaseCallback 返回onRelease回调结果。

错误码:

错误码ID 错误信息
401 If the input parameter is not valid parameter.
16200001 Caller released. The caller has been released.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';

  let caller: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        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: ${error.code}, error.message: ${error.message}');
          }
      }).catch((err: BusinessError) => {
        console.log('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Caller.off

off(type: ‘release’, callback: OnReleaseCallback): void;

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

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

参数:

参数名 类型 必填 说明
type string 监听releaseCall事件,固定为’release’。
callback OnReleaseCallback 返回off回调结果。

错误码:

错误码ID 错误信息
401 If the input parameter is not valid parameter.

示例:

  import UIAbility, { OnReleaseCallback } from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';

  let caller: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        deviceId: ''
      }).then((obj) => {
          caller = obj;
          try {
            let onReleaseCallBack: 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: ${error.code}, error.message: ${error.message}');
          }
      }).catch((err: BusinessError) => {
        console.log('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Caller.off

off(type: ‘release’): void;

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

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

参数:

参数名 类型 必填 说明
type string 监听releaseCall事件,固定为’release’。

示例:

  import UIAbility, { OnReleaseCallback } from '@ohos.app.ability.UIAbility';
  import { Caller } from '@ohos.app.ability.UIAbility';
  import { BusinessError } from '@ohos.base';
  import window from '@ohos.window';

  let caller: Caller;
  export default class MainUIAbility extends UIAbility {
    onWindowStageCreate(windowStage: window.WindowStage) {
      this.context.startAbilityByCall({
        bundleName: 'com.example.myservice',
        abilityName: 'MainUIAbility',
        deviceId: ''
      }).then((obj) => {
          caller = obj;
          try {
            let onReleaseCallBack: 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: ${error.code}, error.message: ${error.message}');
          }
      }).catch((err: BusinessError) => {
        console.error('Caller GetCaller error, error.code: ${err.code}, error.message: ${err.message}');
      });
    }
  }

Callee

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

Callee.on

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

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

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

参数:

参数名 类型 必填 说明
method string 与客户端约定的通知消息字符串。
callback CalleeCallback 一个rpc.MessageSequence类型入参的js通知同步回调函数,&nbsp;回调函数至少要返回一个空的rpc.Parcelable数据对象,&nbsp;其他视为函数执行错误。

错误码:

错误码ID 错误信息
16200004 Method registered. The method has registered.
16000050 Internal error.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility, { CalleeCallback } from '@ohos.app.ability.UIAbility';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  import Want from '@ohos.app.ability.Want';
  import rpc from '@ohos.rpc';

  class MyMessageAble implements rpc.Parcelable {
      name: string
      str: string
      num: number = 1
      constructor(name: string, str: string) {
        this.name = name;
        this.str = str;
      }
      marshalling(messageSequence: rpc.MessageSequence) {
          messageSequence.writeInt(this.num);
          messageSequence.writeString(this.str);
          console.log('MyMessageAble marshalling num[${this.num}] str[${this.str}]');
          return true;
      }
      unmarshalling(messageSequence: rpc.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: rpc.MessageSequence) {
      console.log('Callee funcCallBack is called ${pdata}');
      let msg = new MyMessageAble('test', '');
      pdata.readParcelable(msg);
      return new MyMessageAble('test1', 'Callee test');
  }
  export default class MainUIAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
      console.log('Callee onCreate is called');
      try {
        this.callee.on(method, funcCallBack);
      } catch (error) {
        console.log('Callee.on catch error, error.code: ${error.code}, error.message: ${error.message}');
      }
    }
  }

Callee.off

off(method: string): void;

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

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

参数:

参数名 类型 必填 说明
method string 已注册的通知事件字符串。

错误码:

错误码ID 错误信息
16200005 Method not registered. The method has not registered.
16000050 Internal error.

以上错误码详细介绍请参考errcode-ability

示例:

  import UIAbility from '@ohos.app.ability.UIAbility';
  import AbilityConstant from '@ohos.app.ability.AbilityConstant';
  import Want from '@ohos.app.ability.Want';

  let method = 'call_Function';
  export default class MainUIAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
      console.log('Callee onCreate is called');
      try {
        this.callee.off(method);
      } catch (error) {
        console.log('Callee.off catch error, error.code: ${error.code}, error.message: ${error.message}');
      }
    }
  }

OnReleaseCallback

(msg: string): void;

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

名称 可读 可写 类型 说明
(msg: string) function 调用者注册的侦听器函数接口的原型。

OnRemoteStateChangeCallback10+

(msg: string): void;

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

名称 可读 可写 类型 说明
(msg: string) function 调用者注册的协同场景下组件状态变化监听函数接口的原型。

CalleeCallback

(indata: rpc.MessageSequence): rpc.Parcelable;

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

名称 可读 可写 类型 说明
(indata: rpc.MessageSequence) rpc.Parcelable 被调用方注册的消息侦听器函数接口的原型。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞