faqs-bundle-management.md 5.7 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8
# 包管理开发常见问题

## 如何判断某个应用是否为系统应用

适用于OpenHarmony 3.2 Beta5  API 9

**解决措施**

W
wusongqing 已提交
9
使用bundleManager模块的getApplicationInfo接口(仅系统应用可以使用)获取待检验应用的ApplicationInfo,根据ApplicationInfo中systemApp字段判断,若为true,则是系统应用,否则为非系统应用。
Z
zengyawen 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

**参考链接**

[bundleManager模块](../reference/apis/js-apis-bundleManager.md)

## 如何获取应用配置的versionCode和versionName

适用于:Openharmony 3.2 Beta5 API 9

**解决措施**

首先通过@ohos.bundle.bundleManager模块bundleManager.getBundleInfoForSelf\(\)接口获取包信息BundleInfo,然后分别通过BundleInfo.versionCode、BundleInfo.versionName获取所需信息。

**代码示例**

```
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);
}
```

**参考链接**

[getBundleInfoForSelf](../reference/apis/js-apis-bundleManager.md#bundlemanagergetbundleinfoforself)

## 如何获取应用自身的bundleName

适用于:Openharmony 3.2 Beta5 API9

**解决措施**

可以通过UIAbilityContext.abilityInfo.bundleName获取。

**代码示例**

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

**参考链接**

[UIAbilityContext](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontext)[AbilityInfo](../reference/apis/js-apis-bundleManager-abilityInfo.md#abilityinfo)

## 如何获取App版本号,版本名,屏幕分辨率等信息

适用于:OpenHarmony 3.2 Beta5 

**解决措施**

1.  通过@ohos.bundle.bundleManager模块中的bundleManager查询bundleInfo。

    在bundleInfo中包含App版本号、版本名信息。

    ```
    import bundleManager from '@ohos.bundle.bundleManager';
    ...
    bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo)=>{
      let versionName = bundleInfo.versionName;//应用版本名
      let versionNo = bundleInfo.versionCode;//应用版本号
    }).catch((error)=>{
      console.error("get bundleInfo failed,error is "+error)
    })
    ```

2.  在模块@ohos.app.ability.Configuration中获取screenDensity,其中包含屏幕分辨率信息。

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


## 如何获取应用自身的源文件路径

适用于:OpenHarmony 3.2 Beta5 API 9

**解决措施**

-   方式一:使用应用上下文context获取。

    ```
    this.uiAbilityContext.abilityInfo.applicationInfo.codePath
    ```

-   方式二:使用@ohos.bundle.bundleManager获取。

    1.  导入@ohos.bundle.bundleManager模块,使用bundleManager.getBundleInfoForSelf\(\)获取bundleInfo信息。
    2.  使用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;
    })
    ```


## 能否在本应用中获取到其他应用的HAP包信息

Z
zengyawen 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
适用于:OpenHarmony 3.2 API 9

**问题现象**

如何查询系统内其他应用的信息

**解决措施**

查询系统内其他应用信息的API暂时只提供给系统应用使用,具体使用方法:

- 查询系统内指定应用信息需要获取normal级权限ohos.permission.GET\_BUNDLE\_INFO,使用接口bundleManager.getApplicationInfo\(\)

- 查询系统内所有应用信息需要获取system\_basic级权限ohos.permission.GET\_BUNDLE\_INFO\_PRIVILEGED,使用接口bundleManager.getAllApplicationInfo\(\)

**参考链接**

[@ohos.bundle.bundleManager \(bundleManager模块\)](../reference/apis/js-apis-bundleManager.md)

## 如何查询进程的pid

适用于:OpenHarmony 3.2 Beta API 9 

**解决措施**

可以通过接口@ohos.process来获取。

**代码示例**

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

**参考链接**

[@ohos.process \(获取进程相关的信息\)](../reference/apis/js-apis-process.md)

## 如何让最大化按钮不可用

适用于:OpenHarmony 3.2 Beta API 9 

**解决措施**

可以通过supportWindowModes字段去指定是否显示。

- full\_screen表示支持全屏显示

- split表示支持分屏显示

- floating表示支持窗口化显示

**代码示例**

```
"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"
        ]
      }
    ]
  }
]
```

**参考链接**

[supportWindowModes参考文档](../reference/apis/js-apis-bundleManager-abilityInfo.md)
Z
zengyawen 已提交
206