harmony 鸿蒙@ohos.application.StaticSubscriberExtensionContext (StaticSubscriberExtensionContext)

  • 2023-06-24
  • 浏览 (642)

@ohos.application.StaticSubscriberExtensionContext (StaticSubscriberExtensionContext)

StaticSubscriberExtensionContext模块是StaticSubscriberExtensionAbility的上下文环境,继承自ExtensionContext。

StaticSubscriberExtensionContext模块提供StaticSubscriberExtensionAbility具有的接口和能力。

说明:

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

导入模块

import StaticSubscriberExtensionContext from '@ohos.application.StaticSubscriberExtensionContext';

使用说明

在使用StaticSubscriberExtensionContext的功能前,需要通过StaticSubscriberExtensionAbility获取。

import StaticSubscriberExtensionAbility from '@ohos.application.StaticSubscriberExtensionAbility';
import StaticSubscriberExtensionContext from '@ohos.application.StaticSubscriberExtensionContext';

StaticSubscriberExtensionContext.startAbility

startAbility(want: Want, callback: AsyncCallback<void>): void;

拉起一个静态订阅所属的同应用的Ability。使用callback异步回调。

使用规则: - 调用方应用位于后台时,使用该接口启动Ability需申请ohos.permission.START_ABILITIES_FROM_BACKGROUND权限 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请ohos.permission.START_INVISIBLE_ABILITY权限

需要权限:ohos.permission.START_ABILITIES_FROM_BACKGROUND

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

系统API:该接口为系统接口,三方应用不支持调用。

参数:

参数名 类型 必填 说明
want Want 启动Ability的want信息。
callback AsyncCallback<void> callback形式返回启动结果。

错误码:

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

错误码ID 错误信息
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000011 The context does not exist.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16200001 The caller has been released.
16300003 The target application is not self application.

示例:

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

let want: Want = {
  bundleName: "com.example.myapp",
  abilityName: "MyAbility"
};

class MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility {
  onReceiveEvent(event: CommonEventManager.CommonEventData) {
    console.log(`onReceiveEvent, event: ${JSON.stringify(event)}`);

    try {
      this.context.startAbility(want, (error: BusinessError) => {
        if (error) {
          // 处理业务逻辑错误
          console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
            ' error.message: ' + JSON.stringify(error.message));
          return;
        }
        // 执行正常业务
        console.log('startAbility succeed');
      });
    } catch (paramError) {
      // 处理入参错误异常
      let code = (paramError as BusinessError).code;
      let message = (paramError as BusinessError).message;
      console.log('startAbility failed, error.code: ' + JSON.stringify(code) +
        ' error.message: ' + JSON.stringify(message));
    }
  }
}

StaticSubscriberExtensionContext.startAbility

startAbility(want: Want): Promise<void>;

拉起一个静态订阅所属的同应用的Ability。使用Promise异步回调。

使用规则: - 调用方应用位于后台时,使用该接口启动Ability需申请ohos.permission.START_ABILITIES_FROM_BACKGROUND权限 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请ohos.permission.START_INVISIBLE_ABILITY权限

需要权限:ohos.permission.START_ABILITIES_FROM_BACKGROUND

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

系统API:该接口为系统接口,三方应用不支持调用。

参数:

参数名 类型 必填 说明
want Want 启动Ability的want信息。

返回值:

类型 说明
Promise<void> Promise形式返回启动结果。

错误码:

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

错误码ID 错误信息
16000001 The specified ability does not exist.
16000002 Incorrect ability type.
16000004 Can not start invisible component.
16000005 The specified process does not have the permission.
16000006 Cross-user operations are not allowed.
16000008 The crowdtesting application expires.
16000009 An ability cannot be started or stopped in Wukong mode.
16000011 The context does not exist.
16000050 Internal error.
16000053 The ability is not on the top of the UI.
16000055 Installation-free timed out.
16200001 The caller has been released.
16300003 The target application is not self application.

示例:

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

let want: Want = {
  bundleName: "com.example.myapp",
  abilityName: "MyAbility"
};

class MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility {
  onReceiveEvent(event: CommonEventManager.CommonEventData) {
    console.log(`onReceiveEvent, event: ${JSON.stringify(event)}`);
    try {
      this.context.startAbility(want)
        .then(() => {
          // 执行正常业务
          console.log('startAbility succeed');
        })
        .catch((error: BusinessError) => {
          // 处理业务逻辑错误
          console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
            ' error.message: ' + JSON.stringify(error.message));
        });
    } catch (paramError) {
      // 处理入参错误异常
      let code = (paramError as BusinessError).code;
      let message = (paramError as BusinessError).message;
      console.log('startAbility failed, error.code: ' + JSON.stringify(code) +
        ' error.message: ' + JSON.stringify(message));
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

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

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

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

harmony 鸿蒙BundleStatusCallback

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

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

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

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

0  赞