openharmony 鸿蒙 js-apis-inner-application-applicationContext-sys

2025-06-12 浏览 (1)

ApplicationContext (System API)

The ApplicationContext module, inherited from Context, provides application-level context capabilities, including APIs for registering and deregistering the lifecycle of application components.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This topic describes only system APIs provided by the module. For details about its public APIs, see ApplicationContext.

Modules to Import

import { common } from '@kit.AbilityKit';

Instructions

Before calling any APIs in ApplicationContext, obtain an ApplicationContext instance through the context instance.

ApplicationContext.preloadUIExtensionAbility12+

preloadUIExtensionAbility(want: Want): Promise<void>

Preloads a UIExtensionAbility instance.

The preloaded UIExtensionAbility instance is sent to the onCreate lifecycle of the UIExtensionAbility and waits to be loaded by the current application.

A UIExtensionAbility instance can be preloaded for multiple times. Each time a preloaded UIExtensionAbility instance is loaded, the next preloaded UIExtensionAbility instance is sent to the onCreate lifecycle of the UIExtensionAbility.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API.

NameTypeMandatoryDescription
wantWantYesWant information of the UIExtensionAbility.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

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

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
16000001The specified ability does not exist.
16000002Incorrect ability type.
16000004Failed to start the invisible ability.
16200011The context does not exist.
16000050Internal error.

Example

import { UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate() {
    let want: Want = {
      bundleName: 'com.ohos.uiextensionprovider',
      abilityName: 'UIExtensionProvider',
      moduleName: 'entry',
      parameters: {
        // The value must be the same as the value of type in the module.json5 file of the UIExtensionAbility.
        'ability.want.params.uiExtensionType': 'sys/commonUI'
      }
    };
    try {
      let applicationContext = this.context.getApplicationContext();
      applicationContext.preloadUIExtensionAbility(want)
        .then(() => {
          // Carry out normal service processing.
          console.info('preloadUIExtensionAbility succeed');
        })
        .catch((err: BusinessError) => {
          // Process service logic errors.
          console.error('preloadUIExtensionAbility failed');
        });
    } catch (err) {
      // Process input parameter errors.
      let code = (err as BusinessError).code;
      let message = (err as BusinessError).message;
      console.error(`preloadUIExtensionAbility failed. code: ${code}, msg: ${message}`);
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

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