harmony 鸿蒙Creating a PageAbility
Creating a PageAbility
When you create a PageAbility in DevEco Studio, DevEco Studio automatically generates the onCreate() and onDestroy() callbacks in app.js and app.ets. You need to implement the other lifecycle callbacks in app.js and app.ets. The following code snippet shows how to create a PageAbility:
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');
}
onDestroy() {
hilog.info(domain, TAG, 'Application onDestroy');
}
onShow(): void {
hilog.info(domain, TAG, 'Application onShow');
}
onHide(): void {
hilog.info(domain, TAG, 'Application onHide');
}
onActive(): void {
hilog.info(domain, TAG, 'Application onActive');
}
onInactive(): void {
hilog.info(domain, TAG, 'Application onInactive');
}
onNewWant() {
hilog.info(domain, TAG, 'Application onNewWant');
}
}
export default new MainAbility();
After the PageAbility is created, its abilities-related configuration items are displayed in the config.json file. The following is an example config.json file of an ability named EntryAbility:
{
...
"module": {
...
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "unspecified",
"formsEnabled": false,
"name": ".MainAbility",
"srcLanguage": "ets",
"srcPath": "MainAbility",
"icon": "$media:icon",
"description": "$string:MainAbility_desc",
"label": "$string:MainAbility_label",
"type": "page",
"visible": true,
"launchType": "singleton"
},
...
]
...
}
}
In the FA model, you can call getContext of featureAbility to obtain the application context and then use the capabilities provided by the context.
Table 1 featureAbility APIs
API | Description |
---|---|
getContext() | Obtains the application context. |
The following code snippet shows how to use getContext() to obtain the application context and distributed directory:
import featureAbility from '@ohos.ability.featureAbility';
import fs from '@ohos.file.fs';
import promptAction from '@ohos.promptAction';
import hilog from '@ohos.hilog';
const TAG: string = 'PagePageAbilityFirst';
const domain: number = 0xFF00;
(async (): Promise<void> => {
let dir: string;
try {
hilog.info(domain, TAG, 'Begin to getOrCreateDistributedDir');
dir = await featureAbility.getContext().getOrCreateDistributedDir();
promptAction.showToast({
message: dir
});
hilog.info(domain, TAG, 'distribute dir is ' + dir);
let fd: number;
let path = dir + '/a.txt';
fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd;
fs.close(fd);
} catch (error) {
hilog.error(domain, TAG, 'getOrCreateDistributedDir failed with : ' + error);
}
})()
你可能感兴趣的鸿蒙文章
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)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦