1# FA模型的Context
2
3
4FA模型下只有一个Context。Context中的所有功能都是通过方法来提供的,它提供了一些featureAbility中不存在的方法,相当于featureAbility的一个扩展和补全。
5
6
7## 接口说明
8
9FA模型下使用Context,需要通过featureAbility下的接口getContext来获取,而在此之前,需要先导入对应的包:
10
11
12```ts
13import featureAbility from '@ohos.ability.featureAbility';
14```
15
16然后使用如下方式获取对应的Context对象:
17
18
19```ts
20import featureAbility from '@ohos.ability.featureAbility';
21
22let context = featureAbility.getContext();
23```
24
25最终返回的对象为Context,其对应的接口说明请参见[接口文档](../reference/apis-ability-kit/js-apis-inner-app-context.md)。
26
27
28## 开发步骤
29
301. 查询Bundle信息。
31
32    ```ts
33    import featureAbility from '@ohos.ability.featureAbility';
34    import hilog from '@ohos.hilog';
35
36    const TAG: string = 'MainAbility';
37    const domain: number = 0xFF00;
38
39    class MainAbility {
40      onCreate() {
41        // 获取context并调用相关方法
42        let context = featureAbility.getContext();
43        context.getBundleName((data, bundleName) => {
44          hilog.info(domain, TAG, 'ability bundleName:' + bundleName);
45        });
46        hilog.info(domain, TAG, 'Application onCreate');
47      }
48      //...
49    }
50
51    export default new MainAbility();
52    ```
53
542. 设置当前featureAbility的显示方向。
55
56    ```ts
57    import featureAbility from '@ohos.ability.featureAbility';
58    import bundle from '@ohos.bundle';
59    import hilog from '@ohos.hilog';
60
61    const TAG: string = 'PageAbilitySingleton';
62    const domain: number = 0xFF00;
63
64    class PageAbilitySingleton {
65      onCreate() {
66        // 获取context并调用相关方法
67        let context = featureAbility.getContext();
68        context.setDisplayOrientation(bundle.DisplayOrientation.PORTRAIT).then(() => {
69          hilog.info(domain, TAG, 'Set display orientation.');
70        })
71        hilog.info(domain, TAG, 'Application onCreate');
72      }
73
74      onDestroy() {
75        hilog.info(domain, TAG, 'Application onDestroy');
76      }
77      //...
78    }
79
80    export default new PageAbilitySingleton();
81    ```
82