1# 包管理开发常见问题 2 3 4## 如何判断某个应用是否为系统应用(API 9) 5 6**解决措施** 7 8使用bundleManager模块的getApplicationInfo接口(仅系统应用可以使用)获取待检验应用的ApplicationInfo,根据ApplicationInfo中systemApp字段判断,若为true,则是系统应用,否则为非系统应用。 9 10**参考链接** 11 12[bundleManager模块](../reference/apis-ability-kit/js-apis-bundleManager.md) 13 14 15## 如何获取应用配置的versionCode和versionName(API 9) 16 17**解决措施** 18 19首先通过\@ohos.bundle.bundleManager模块bundleManager.getBundleInfoForSelf()接口获取包信息BundleInfo,然后分别通过BundleInfo.versionCode、BundleInfo.versionName获取所需信息。 20 21**代码示例** 22 23``` 24import bundleManager from '@ohos.bundle.bundleManager'; 25import hilog from '@ohos.hilog'; 26let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT; 27try { 28 bundleManager.getBundleInfoForSelf(bundleFlags).then((data) => { 29 const versionCode = data.versionCode; 30 const versionName = data.versionName; 31 hilog.info(0x0000, 'testTag', `successfully. versionCode: ${versionCode}, versionName: ${versionName}`); 32 }).catch(err => { 33 hilog.error(0x0000, 'testTag', 'failed. Cause: %{public}s', err.message); 34 }); 35} catch (err) { 36 hilog.error(0x0000, 'testTag', 'failed: %{public}s', err.message); 37} 38``` 39 40**参考链接** 41 42[getBundleInfoForSelf](../reference/apis-ability-kit/js-apis-bundleManager.md#bundlemanagergetbundleinfoforself) 43 44 45## 如何获取应用自身的bundleName(API 9) 46 47**解决措施** 48 49可以通过UIAbilityContext.abilityInfo.bundleName获取。 50 51**代码示例** 52 53``` 54import common from '@ohos.app.ability.common'; 55const context = getContext(this) as common.UIAbilityContext 56console.log(`bundleName: ${context.abilityInfo.bundleName}`) 57``` 58 59**参考链接** 60 61[UIAbilityContext](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)、[AbilityInfo](../reference/apis-ability-kit/js-apis-bundleManager-abilityInfo.md) 62 63 64## 如何获取App版本号,版本名,屏幕分辨率等信息(API 9) 65 66**解决措施** 67 681. 通过\@ohos.bundle.bundleManager模块中的bundleManager查询bundleInfo。 69 70 在bundleInfo中包含App版本号、版本名信息。 71 72 ``` 73 import bundleManager from '@ohos.bundle.bundleManager'; 74 ... 75 bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo)=>{ 76 let versionName = bundleInfo.versionName;//应用版本名 77 let versionNo = bundleInfo.versionCode;//应用版本号 78 }).catch((error)=>{ 79 console.error("get bundleInfo failed,error is "+error) 80 }) 81 ``` 82 832. 在模块\@ohos.app.ability.Configuration中获取screenDensity,其中包含屏幕分辨率信息。 84 85 ``` 86 import common from '@ohos.app.ability.common'; 87 ... 88 let context = getContext(this) as common.UIAbilityContext; 89 let screenDensity = context.config.screenDensity; 90 ``` 91 92 93## 如何获取应用自身的源文件路径(API 9) 94 95**解决措施** 96 97- 方式一:使用应用上下文context获取。 98 99 ``` 100 this.uiAbilityContext.abilityInfo.applicationInfo.codePath 101 ``` 102 103- 方式二:使用\@ohos.bundle.bundleManager获取。 104 105 1. 导入\@ohos.bundle.bundleManager模块,使用bundleManager.getBundleInfoForSelf()获取bundleInfo信息。 106 2. 使用bundleInfo.appInfo.codePath获取应用源文件路径。 107 108 ``` 109 import bundleManager from '@ohos.bundle.bundleManager'; 110 bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo)=>{ 111 this.sourcePath = bundleInfo.appInfo.codePath; 112 }) 113 ``` 114 115 116## 能否在本应用中获取到其他应用的HAP包信息(API 9) 117 118**问题现象** 119 120如何查询系统内其他应用的信息 121 122**解决措施** 123 124查询系统内其他应用信息的API暂时只提供给系统应用使用,具体使用方法: 125 126- 查询系统内指定应用信息需要获取normal级权限ohos.permission.GET_BUNDLE_INFO,使用接口bundleManager.getApplicationInfo()。 127 128- 查询系统内所有应用信息需要获取system_basic级权限ohos.permission.GET_BUNDLE_INFO_PRIVILEGED,使用接口bundleManager.getAllApplicationInfo()。 129 130**参考链接** 131 132[@ohos.bundle.bundleManager \(bundleManager模块\)](../reference/apis-ability-kit/js-apis-bundleManager.md) 133 134 135## 如何查询进程的pid(API 9) 136 137**解决措施** 138 139可以通过接口\@ohos.process来获取。 140 141**代码示例** 142 143``` 144import process from '@ohos.process'; 145private pid = process.pid; 146``` 147 148**参考链接** 149 150[@ohos.process (获取进程相关的信息)](../reference/apis-arkts/js-apis-process.md) 151 152 153## 如何让最大化按钮不可用(API 9) 154 155**解决措施** 156 157可以通过supportWindowModes字段去指定是否显示。 158 159- full_screen表示支持全屏显示 160 161- split表示支持分屏显示 162 163- floating表示支持窗口化显示 164 165**代码示例** 166 167``` 168"abilities": [ 169 { 170 "name": "EntryAbility", 171 "srcEntry": "./ets/entryability/EntryAbility.ets", 172 "description": "$string:EntryAbility_desc", 173 "icon": "$media:icon", 174 "label": "$string:EntryAbility_label", 175 "startWindowIcon": "$media:icon", 176 "startWindowBackground": "$color:start_window_background", 177 "exported": true, 178 "supportWindowMode": ["split", "floating"], 179 "skills": [ 180 { 181 "entities": [ 182 "entity.system.home" 183 ], 184 "actions": [ 185 "action.system.home" 186 ] 187 } 188 ] 189 } 190] 191``` 192 193**参考链接** 194 195[supportWindowModes参考文档](../reference/apis-ability-kit/js-apis-bundleManager-abilityInfo.md) 196 197