harmony 鸿蒙Context (FA Model)

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

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'

    class Entry {
      onCreate() {
        // Obtain the context and call related APIs.
        let context = featureAbility.getContext();
        context.getBundleName((data, bundleName)=>{
          console.info("ability bundleName:" + bundleName)
        });
        console.info('Application onCreate')
      }
      onDestroy() {
        console.info('Application onDestroy')
      }
    }

    export default new Entry()
  1. Set the display orientation of the featureAbility.
    import featureAbility from '@ohos.ability.featureAbility'
    import bundleManager from '@ohos.bundle.bundleManager';

    class Entry {
      onCreate() {
        // Obtain the context and call related APIs.
        let context = featureAbility.getContext();
        context.setDisplayOrientation(bundleManager.DisplayOrientation.LANDSCAPE).then(() => {
          console.info("Set display orientation.")
        })
        console.info('Application onCreate')
      }
      onDestroy() {
        console.info('Application onDestroy')
      }
    }

    export default new Entry();

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞