HarmonyOS鸿蒙Next中如何解决获取桌面APP的名称错误的问题
HarmonyOS鸿蒙Next中如何解决获取桌面APP的名称错误的问题
【问题现象】
用户使用@ohos.bundle.bundleManager
里的接口getBundleInfoForSelf
,获取的APP信息中 "label": "$string:app_name"
,不是桌面APP的名称。
【背景知识】
- 使用getBundleInfoForSelf获取的是
AppScope
中app.json
中的label
,这个是APP在系统中的名称。 - 使用abilityInfo获取的是
module.json5
文件中配置的label
,这个是APP在桌面中的名称。
【定位思路】
首先需要正确理解桌面APP名称和系统APP名称的区别,桌面APP名称是应用在桌面上显示的名称,是面向用户的应用名称标识,名称配置的位置是AppScope
中app.json
中的label
。而系统APP名称目前没有在页面显示,是面向系统的一个标识,名称配置的位置是module.json5
文件中的label
。二者获取的方法不一样。
【解决方案】
方案一: app.json
文件中的label
可以通过getBundleInfoForSelf
去获取,module.json5
文件中的label
可以通过abilityInfo
去获取。实现方法如下示例所示。
方案二: 系统APP名称和桌面APP名称都可以通过资源管理的接口getStringSync获取。实现方法如下示例所示。
代码示例如下:
import common from '@ohos.app.ability.common';
import { bundleManager } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State appNameOne: string = '';
@State appNameTwo: string = '';
@State windowAppNameOne: string = '';
@State windowAppNameTwo: string = '';
aboutToAppear(): void {
const context = getContext(this) as common.UIAbilityContext
// 获取系统APP的名称
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION
let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleFlags);
let appLabel: string = bundleInfo.appInfo.label
let appRes = appLabel.split(':')[1]
this.appNameOne = context.resourceManager.getStringByNameSync(appRes);
console.log('方案一:获取系统APP的名称:', this.appNameOne);
this.appNameTwo = context.resourceManager.getStringSync($r('app.string.app_name').id);
console.log('方案二:获取系统APP的名称:', this.appNameTwo );
// 获取桌面APP的名称
let windowAppLabel = context.abilityInfo.label
let windowAppRes = windowAppLabel.split(':')[1]
this.windowAppNameOne = context.resourceManager.getStringByNameSync(windowAppRes);
console.log('方案一:获取桌面APP的名称:', this.windowAppNameOne);
this.windowAppNameTwo = context.resourceManager.getStringSync($r('app.string.EntryAbility_label').id);
console.log('方案二:获取桌面APP的名称:', this.windowAppNameTwo);
}
build() {
Row() {
Column() {
Text('方案一:系统的APP名称: ' + this.appNameOne)
.fontSize(20)
Text('方案二:系统的APP名称: ' + this.appNameTwo)
.fontSize(20)
Text('方案一:桌面的APP名称: ' + this.windowAppNameOne)
.fontSize(20)
Text('方案二:桌面的APP名称: ' + this.windowAppNameTwo)
.fontSize(20)
}
.width('100%')
}
.height('100%')
}
}
打印日志如下:
验证图示和桌面APP图示如下:
分析系统APP的名称文件如下:
分析桌面APP的名称文件如下:
【总结】
桌面APP的名称是在module.json5
文件中配置的label
。系统APP的名称是app.json5
文件中配置的label
。
更多关于HarmonyOS鸿蒙Next中如何解决获取桌面APP的名称错误的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,获取桌面APP名称错误可能是由于权限问题或API调用不当。确保应用已申请ohos.permission.GET_BUNDLE_INFO
权限,并在config.json
中正确配置。使用BundleManager
的getBundleInfo
方法时,确保传入的bundleName
参数准确无误。若问题仍存在,检查系统版本是否支持相关API。
更多关于HarmonyOS鸿蒙Next中如何解决获取桌面APP的名称错误的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中获取桌面APP名称时,若出现获取错误的情况,通常是因为混淆了系统APP名称和桌面APP名称的获取方式。以下是解决方案:
1. 关键区别
- 系统APP名称:位于AppScope/app.json中的label,使用getBundleInfoForSelf获取
- 桌面APP名称:位于module.json5中的label,通过abilityInfo获取
2. 推荐解决方案
方案一(推荐)
// 获取系统APP名称
const bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
const sysAppName = context.resourceManager.getStringByNameSync(bundleInfo.appInfo.label.split(':')[1]);
// 获取桌面APP名称
const desktopAppName = context.resourceManager.getStringByNameSync(context.abilityInfo.label.split(':')[1]);
方案二(资源ID方式)
// 系统APP名称
const sysAppName = context.resourceManager.getStringSync($r('app.string.app_name').id);
// 桌面APP名称
const desktopAppName = context.resourceManager.getStringSync($r('app.string.EntryAbility_label').id);
3. 注意事项
- 确保module.json5中配置了正确的EntryAbility_label
- 两种方案都需要UIAbilityContext上下文
- 方案二需要提前知道资源ID的准确路径
建议优先使用方案一,因为它直接读取配置文件,可靠性更高。如果遇到问题,可以检查对应json文件中的label配置是否正确。