harmony 鸿蒙Bundle Management Development

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

Bundle Management Development

How do I determine whether an application is a system application?

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Use bundleManager.getApplicationInfo (available only for system applications) to obtain application information, and check the value of systemApp in the information. The application is a system application if the value is true.

Reference

bundleManager

How do I obtain the version code and version name of an application?

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Use bundleManager.getBundleInfoForSelf() to obtain the bundle information, which contains the version code (specified by BundleInfo.versionCode) and version name (specified by BundleInfo.versionName) .

Example

import bundleManager from '@ohos.bundle.bundleManager';
import hilog from '@ohos.hilog';
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
try {
  bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => {
    const versionCode = data.versionCode;
    const versionName = data.versionName;
    hilog.info(0x0000, 'testTag', `successfully. versionCode: ${versionCode}, versionName: ${versionName}`);    
  }).catch(err => {
    hilog.error(0x0000, 'testTag', 'failed. Cause: %{public}s', err.message);
  });
} catch (err) {
  hilog.error(0x0000, 'testTag', 'failed: %{public}s', err.message);
}

Reference

getBundleInfoForSelf

How do I obtain the bundle name of the current application?

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Obtain the bundle name from UIAbilityContext.abilityInfo.bundleName.

Example

import common from '@ohos.app.ability.common';
const context = getContext(this) as common.UIAbilityContext
console.log(`bundleName: ${context.abilityInfo.bundleName}`)

Reference

UIAbilityContext and AbilityInfo

How do I obtain the application version number, version name, and screen resolution?

Applicable to: OpenHarmony 3.2 Beta5

Solution

  1. Obtain the bundle information from the @ohos.bundle.bundleManager module.

    The bundle information contains the application version number and version name.

    import bundleManager from '@ohos.bundle.bundleManager';
    ...
    bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo)=>{
      let versionName = bundleInfo.versionName; // Application version name.
      let versionNo = bundleInfo.versionCode; // Application version number.
    }).catch((error)=>{
      console.error("get bundleInfo failed,error is "+error)
    })
    
  2. Obtain screenDensity from the @ohos.app.ability.Configuration module. screenDensity contains the screen resolution information.

    import common from '@ohos.app.ability.common';
    ...
    let context = getContext(this) as common.UIAbilityContext;
    let screenDensity = context.config.screenDensity;
    

How do I obtain the source file path of the current application?

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

  • Method 1: Use the application context to obtain the source file path.

    this.uiAbilityContext.abilityInfo.applicationInfo.codePath
    
  • Method 2: Use @ohos.bundle.bundleManager to obtain the source file path.

    1. Import the @ohos.bundle.bundleManager module and use bundleManager.getBundleInfoForSelf() to obtain the bundle information.
    2. Obtain the source file path from bundleInfo.appInfo.codePath.
    import bundleManager from '@ohos.bundle.bundleManager';
    bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo)=>{
      this.sourcePath = bundleInfo.appInfo.codePath;
    })
    

Can I obtain the HAP information of other applications from the current application?

Applicable to: OpenHarmony 3.2 (API version 9)

Solution

Currently, only system applications can call the API to query information about other applications.

  • To query information about an application in the system, you must obtain the normal-level permission ohos.permission.GET_BUNDLE_INFO and call the bundleManager.getApplicationInfo() API.

  • To query information about all applications in the system, you must obtain the system_basic-level permission ohos.permission.GET_BUNDLE_INFO_PRIVILEGED and call the bundleManager.getAllApplicationInfo() API.

Reference

@ohos.bundle.bundleManager (bundleManager)

How do I query the PID of a process?

Applicable to: OpenHarmony 3.2 Beta (API version 9)

Solution

You can obtain the PID through the @ohos.process interface.

Example

import process from '@ohos.process';
private pid = process.pid;

Reference

@ohos.process \ (Obtaining Process Information)

How do I disable the maximize button?

Applicable to: OpenHarmony 3.2 Beta (API version 9)

Solution

You can use the supportWindowModes field to specify whether to display the maximize button.

  • full_screen means that a window in full-screen mode is supported.

  • split means that a window in split-screen mode is supported.

  • floating means that a floating window is supported.

Example

"abilities": [
  {
    "name": "EntryAbility",
    "srcEntry": "./ets/entryability/EntryAbility.ts",
    "description": "$string:EntryAbility_desc",
    "icon": "$media:icon",
    "label": "$string:EntryAbility_label",
    "startWindowIcon": "$media:icon",
    "startWindowBackground": "$color:start_window_background",
    "exported": true,
    "supportWindowMode": ["split", "floating"],
    "skills": [
      {
        "entities": [
          "entity.system.home"
        ],
        "actions": [
          "action.system.home"
        ]
      }
    ]
  }
]

Reference

supportWindowModes

你可能感兴趣的鸿蒙文章

harmony 鸿蒙FAQs

harmony 鸿蒙Using NDK in a CMake Project

harmony 鸿蒙Application Access Control Development

harmony 鸿蒙Application Model Development

harmony 鸿蒙ArkUI Animation/Interaction Event Development (ArkTS)

harmony 鸿蒙ArkTS Syntax Usage

harmony 鸿蒙ArkUI Component Development (ArkTS)

harmony 鸿蒙ArkUI Development (JS)

harmony 鸿蒙ArkUI Layout Development (ArkTS)

harmony 鸿蒙ArkUI Routing/Navigation Development (ArkTS)

0  赞