harmony 鸿蒙Context

2022-12-22 浏览 (1085)

Context

The Context module provides context for abilities or applications. It allows access to application-specific resources.

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.
  • The APIs of this module can be used only in the stage model.

Modules to Import

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

Attributes

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameTypeReadableWritableDescription
resourceManagerresmgr.ResourceManagerYesNoObject for resource management.
applicationInfoApplicationInfoYesNoApplication information.
cacheDirstringYesNoCache directory.
tempDirstringYesNoTemporary directory.
filesDirstringYesNoFile directory.
databaseDirstringYesNoDatabase directory.
preferencesDirstringYesNoPreferences directory.
bundleCodeDirstringYesNoBundle code directory. Do not access resource files by concatenating paths. Use the resourceManager API instead.
distributedFilesDirstringYesNoDistributed file directory.
eventHubEventHubYesNoEvent hub that implements event subscription, unsubscription, and triggering.
areacontextConstant.AreaModeYesNoEncryption level of the directory.

Context.createBundleContext

createBundleContext(bundleName: string): Context;

Creates the context based on the bundle name.

Required permissions: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

System capability: SystemCapability.Ability.AbilityRuntime.Core

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

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.

Return value

TypeDescription
ContextContext created.

Example

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

let bundleContext: common.Context;
try {
    bundleContext = this.context.createBundleContext('com.example.test');
} catch (error) {
    console.error('createBundleContext failed, error.code: ${error.code}, error.message: ${error.message}');
}

Context.createModuleContext

createModuleContext(moduleName: string): Context;

Creates the context based on the module name.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
moduleNamestringYesModule name.

Return value

TypeDescription
ContextContext created.

Example

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

let moduleContext: common.Context;
try {
    moduleContext = this.context.createModuleContext('entry');
} catch (error) {
    console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
}

NOTE

Only the context of other modules in the current application and the context of the intra-application HSP can be obtained. The context of other applications cannot be obtained.

Context.createModuleContext

createModuleContext(bundleName: string, moduleName: string): Context;

Creates the context based on the bundle name and module name.

System capability: SystemCapability.Ability.AbilityRuntime.Core

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

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.
moduleNamestringYesModule name.

Return value

TypeDescription
ContextContext created.

Example

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

let moduleContext: common.Context;
try {
    moduleContext = this.context.createModuleContext('com.example.test', 'entry');
} catch (error) {
    console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
}

Context.getApplicationContext

getApplicationContext(): ApplicationContext;

Obtains the context of this application.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Return value

TypeDescription
ApplicationContextApplication context obtained.

Example

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

let applicationContext: common.Context;
try {
    applicationContext = this.context.getApplicationContext();
} catch (error) {
    console.error('getApplicationContext failed, error.code: ${error.code}, error.message: ${error.message}');
}

Context.getGroupDir10+

getGroupDir(dataGroupID: string): Promise<string>;

Obtains the shared directory based on a group ID. This API uses a promise to return the result.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
dataGroupIDstringYesGroup ID, which is assigned by the system when an atomic service application project is created.

Return value

TypeDescription
Promise<string>Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.

Error codes

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

IDError Message
16000011The context does not exist.

Example

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

let groupId = "1";
let getGroupDirContext: common.Context = this.context;
try {
  getGroupDirContext.getGroupDir(groupId).then(data => {
    console.log("getGroupDir result:" + data);
  })
} catch (error) {
  console.error('getGroupDirContext failed, error.code: ${error.code}, error.message: ${error.message}');
}

Context.getGroupDir10+

getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void;

Obtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Parameters

NameTypeMandatoryDescription
dataGroupIDstringYesGroup ID, which is assigned by the system when an atomic service application project is created.
callbackAsyncCallback<string>YesCallback used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.

Error codes

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

IDError Message
16000011The context does not exist.

Example

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

let getGroupDirContext: common.Context = this.context;

getGroupDirContext.getGroupDir("1", (err, data) => {
  if (err) {
    console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`);
  } else {
    console.log(`getGroupDir result is: ${JSON.stringify(data)}`);
  }
});

你可能感兴趣的鸿蒙文章

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/fbcb576143524c5a86d617f337c0b9e5