• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 创建PageAbility
2
3<!--Kit: Ability Kit-->
4<!--Subsystem: Ability-->
5<!--Owner: @lidongrui-->
6<!--Designer: @ccllee1-->
7<!--Tester: @lixueqing513-->
8<!--Adviser: @huipeizi-->
9
10通过DevEco Studio开发平台创建PageAbility时,DevEco Studio会在app.js/app.ets中默认生成onCreate()和onDestroy()方法,其他方法需要开发者自行实现。接口说明参见[PageAbility的生命周期](pageability-lifecycle.md),创建PageAbility示例如下:
11
12```ts
13import featureAbility from '@ohos.ability.featureAbility';
14import hilog from '@ohos.hilog';
15
16const TAG: string = 'MainAbility';
17const domain: number = 0xFF00;
18
19class MainAbility {
20  onCreate() {
21    // 获取context并调用相关方法
22    let context = featureAbility.getContext();
23    context.getBundleName((data, bundleName) => {
24      hilog.info(domain, TAG, 'ability bundleName:' ,bundleName);
25    });
26    hilog.info(domain, TAG, 'Application onCreate');
27  }
28
29  onDestroy() {
30    hilog.info(domain, TAG, 'Application onDestroy');
31  }
32
33  onShow(): void {
34    hilog.info(domain, TAG, 'Application onShow');
35  }
36
37  onHide(): void {
38    hilog.info(domain, TAG, 'Application onHide');
39  }
40
41  onActive(): void {
42    hilog.info(domain, TAG, 'Application onActive');
43  }
44
45  onInactive(): void {
46    hilog.info(domain, TAG, 'Application onInactive');
47  }
48
49  onNewWant() {
50    hilog.info(domain, TAG, 'Application onNewWant');
51  }
52}
53
54export default new MainAbility();
55```
56
57
58PageAbility创建成功后,其abilities相关的配置项在config.json中体现,一个名字为EntryAbility的config.json配置文件示例如下:
59
60```json
61{
62  ...
63  "module": {
64    ...
65    "abilities": [
66      {
67        "skills": [
68          {
69            "entities": [
70              "entity.system.home"
71            ],
72            "actions": [
73              "action.system.home"
74            ]
75          }
76        ],
77        "orientation": "unspecified",
78        "formsEnabled": false,
79        "name": ".MainAbility",
80        "srcLanguage": "ets",
81        "srcPath": "MainAbility",
82        "icon": "$media:icon",
83        "description": "$string:MainAbility_desc",
84        "label": "$string:MainAbility_label",
85        "type": "page",
86        "visible": true,
87        "launchType": "singleton"
88      },
89      ...
90    ]
91    ...
92  }
93}
94```
95
96
97FA模型中,可以通过featureAbility的getContext接口获取应用上下文,进而使用上下文提供的能力。
98
99
100  **表1** featureAbility接口说明
101
102| 接口名 | 接口描述 |
103| -------- | -------- |
104| getContext() | 获取应用上下文。 |
105
106
107通过getContext获取应用上下文并获取分布式目录的示例如下:
108
109```ts
110import featureAbility from '@ohos.ability.featureAbility';
111import fs from '@ohos.file.fs';
112import promptAction from '@ohos.promptAction';
113import hilog from '@ohos.hilog';
114
115const TAG: string = 'PagePageAbilityFirst';
116const domain: number = 0xFF00;
117```
118```ts
119(async (): Promise<void> => {
120  let dir: string;
121  try {
122    hilog.info(domain, TAG, 'Begin to getOrCreateDistributedDir');
123    dir = await featureAbility.getContext().getOrCreateDistributedDir();
124    promptAction.showToast({
125      message: dir
126    });
127    hilog.info(domain, TAG, 'distribute dir is ' + dir);
128    let fd: number;
129    let path = dir + '/a.txt';
130    fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd;
131    fs.close(fd);
132  } catch (error) {
133    hilog.error(domain, TAG, 'getOrCreateDistributedDir failed with : ' + error);
134  }
135})()
136```
137