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

2023-06-24 浏览 (715)

@ohos.application.StaticSubscriberExtensionContext (StaticSubscriberExtensionContext)

The StaticSubscriberExtensionContext module, inherited from ExtensionContext, provides context for StaticSubscriberExtensionAbilities.

You can use the APIs of this module to start StaticSubscriberExtensionAbilities.

NOTE

The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. The APIs of this module can be used only in the stage model.

Modules to Import

import StaticSubscriberExtensionContext from '@ohos.application.StaticSubscriberExtensionContext'

Usage

Before using the StaticSubscriberExtensionContext module, you must first obtain a StaticSubscriberExtensionAbility instance.

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

export default class MyStaticSubscriberExtensionAbility extends StaticSubscriberExtensionAbility {
    context: StaticSubscriberExtensionContext = this.context;
};

StaticSubscriberExtensionContext.startAbility

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

Starts an ability that belongs to the same application as this StaticSubscriberExtensionAbility. This API uses an asynchronous callback to return the result.

Observe the following when using this API:

  • If an application running in the background needs to call this API to start an ability, it must have the ohos.permission.START_ABILITIES_FROM_BACKGROUND permission.
  • If visible of the target ability is false in cross-application scenarios, the caller must have the ohos.permission.START_INVISIBLE_ABILITY permission.

Required permissions: ohos.permission.START_ABILITIES_FROM_BACKGROUND

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
wantWantYesWant information about the target ability.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Ability Error Codes.

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

Example

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

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

try {
  this.context.startAbility(want, (error: BusinessError) => {
    if (error) {
      // Process service logic errors.
      console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
      return;
    }
    // Carry out normal service processing.
    console.log('startAbility succeed');
  });
} catch (paramError) {
  // Process input parameter errors.
  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>;

Starts an ability that belongs to the same application as this StaticSubscriberExtensionAbility. This API uses a promise to return the result.

Observe the following when using this API:

  • If an application running in the background needs to call this API to start an ability, it must have the ohos.permission.START_ABILITIES_FROM_BACKGROUND permission.
  • If visible of the target ability is false in cross-application scenarios, the caller must have the ohos.permission.START_INVISIBLE_ABILITY permission.

Required permissions: ohos.permission.START_ABILITIES_FROM_BACKGROUND

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
wantWantYesWant information about the target ability.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see Ability Error Codes.

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

Example

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

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

try {
  this.context.startAbility(want)
    .then(() => {
      // Carry out normal service processing.
      console.log('startAbility succeed');
    })
    .catch((error: BusinessError) => {
      // Process service logic errors.
      console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
    });
} catch (paramError) {
  // Process input parameter errors.
  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 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

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

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/7e49e7a326324e02a54c95dbab641717