harmony 鸿蒙Context (FA Model)

  • 2023-02-03
  • 浏览 (820)

Context (FA Model)

There is only one context in the FA model. All capabilities in the context are provided through methods. The context uses these methods to extend the capabilities of the featureAbility class.

Available APIs

To use the context in the FA model, first import the featureAbility module.

import featureAbility from '@ohos.ability.featureAbility';

Then, call getContext() to obtain the Context object:

import featureAbility from '@ohos.ability.featureAbility';

let context = featureAbility.getContext();

For details about the APIs, see API Reference.

How to Develop

  1. Query bundle information.

    import featureAbility from '@ohos.ability.featureAbility';
    import hilog from '@ohos.hilog';
    
    
    const TAG: string = 'MainAbility';
    const domain: number = 0xFF00;
    
    
    class MainAbility {
      onCreate() {
        // Obtain the context and call related APIs.
        let context = featureAbility.getContext();
        context.getBundleName((data, bundleName) => {
          hilog.info(domain, TAG, 'ability bundleName:' + bundleName);
        });
        hilog.info(domain, TAG, 'Application onCreate');
      }
      //...
    }
    
    
    export default new MainAbility();
    
  2. Set the display orientation of the featureAbility.

    import featureAbility from '@ohos.ability.featureAbility';
    import bundle from '@ohos.bundle';
    import hilog from '@ohos.hilog';
    
    
    const TAG: string = 'PageAbilitySingleton';
    const domain: number = 0xFF00;
    
    
    class PageAbilitySingleton {
      onCreate() {
        // Obtain the context and call related APIs.
        let context = featureAbility.getContext();
        context.setDisplayOrientation(bundle.DisplayOrientation.PORTRAIT).then(() => {
          hilog.info(domain, TAG, 'Set display orientation.');
        })
        hilog.info(domain, TAG, 'Application onCreate');
      }
    
    
      onDestroy() {
        hilog.info(domain, TAG, 'Application onDestroy');
      }
      //...  
    }
    
    
    export default new PageAbilitySingleton();
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙Obtaining Reasons for Abnormal Application Exits

harmony 鸿蒙UIAbility Backup and Restore

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Introduction to Ability Kit

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙Common action and entities Values (Not Recommended)

harmony 鸿蒙API Switching Overview

0  赞