目录

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

  1. You can obtain the bundle information, including the application version number and version name, through the bundleManager module in @kit.AbilityKit.
    import { BusinessError } from '@kit.BasicServicesKit';
    import { bundleManager } from '@kit.AbilityKit';
    
    // ...
    bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION).then((bundleInfo) => {
      let versionName = bundleInfo.versionName; //App version name
      let versionNo = bundleInfo.versionCode; //App version code
    }).catch((error: BusinessError) => {
      console.error('get bundleInfo failed, error is ' + error);
    })
  2. Obtain screenDensity, which contains the screen resolution information, from context.config.
    import { common } from '@kit.AbilityKit';
    
    // ...
    // In the utility class: Save the context to AppStorage in the EntryAbility - onCreate lifecycle, then use AppStorage to retrieve it in the utility class
    let context = AppStorage.get('context') as common.UIAbilityContext;
    
    let screenDensity = context.config.screenDensity;