AppUtils.ets
1.45 KB
1
2
3
4
5
6
7
8
9
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
import bundleManager from '@ohos.bundle.bundleManager';
import common from '@ohos.app.ability.common';
import { Logger } from './Logger';
const TAG: string = 'AppUtils';
/**
* 与应用相关属性或操作
*/
export class AppUtils {
/**
* 获取应用名称
* 即:咪咕视频
*/
static getAppName(context: common.Context): string {
// todo:获取到的是 $string:app_name
// return context.applicationInfo?.label;
return context.resourceManager.getStringByNameSync("app_name");
}
/**
* 获取应用的包名
* 即:com.cmcc.myapplication
*/
static getPackageName(context: common.Context): string {
return context.applicationInfo?.name;
}
/**
* 获取应用版本号,如:1.0.0.0
* @returns 版本号字符串
*/
static getAppVersionName(): string {
try {
let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT)
return bundleInfo?.versionName
} catch (e) {
Logger.warn(TAG, 'get app version error:' + e?.message);
}
return "";
}
/**
* 获取应用版本编码,如:1000000
* @returns 应用版本编码
*/
static getAppVersionCode(): string {
try {
let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT)
return bundleInfo?.versionCode + ""
} catch (e) {
Logger.warn(TAG, 'get app version error:' + e?.message);
}
return '';
}
}