harmony 鸿蒙创建PageAbility

  • 2022-12-13
  • 浏览 (684)

创建PageAbility

开发者需要重写app.js/app.ets中的生命周期回调函数,开发者通过DevEco Studio开发平台创建PageAbility时,DevEco Studio会在app.js/app.ets中默认生成onCreate()和onDestroy()方法,其他方法需要开发者自行实现。接口说明参见前述章节,创建PageAbility示例如下:

class EntryAbility {
  onCreate() {
    console.info('Application onCreate')
  }
  onDestroy() {
    console.info('Application onDestroy')
  }
  onShow() {
    console.info('Application onShow')
  }
  onHide() {
    console.info('Application onHide')
  }
  onActive() {
    console.info('Application onActive')
  }
  onInactive() {
    console.info('Application onInactive')
  }
  onNewWant() {
    console.info('Application onNewWant')
  }
}

export default new EntryAbility()

PageAbility创建成功后,其abilities相关的配置项在config.json中体现,一个名字为EntryAbility的config.json配置文件示例如下:

{
  "abilities": [
    {
      "skills": [
        {
          "entities": [
            "entity.system.home"
          ],
          "actions": [
            "action.system.home"
          ]
        }
      ],
      "orientation": "unspecified",
      "visible": true,
      "srcPath": "EntryAbility",
      "name": ".EntryAbility",
      "srcLanguage": "ets",
      "icon": "$media:icon",
      "description": "$string:EntryAbility_desc",
      "formsEnabled": false,
      "label": "$string:EntryAbility_label",
      "type": "page",
      "launchType": "singleton"
    }
  ]
}

FA模型中,可以通过featureAbility的getContext接口获取应用上下文,进而使用上下文提供的能力。

表1 featureAbility接口说明

接口名 接口描述
getContext() 获取应用上下文。

通过getContext获取应用上下文并获取分布式目录的示例如下:

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

(async () => {
  let dir: string;
  try {
    console.info('Begin to getOrCreateDistributedDir');
    dir = await featureAbility.getContext().getOrCreateDistributedDir();
    console.info('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) {
    console.error('getOrCreateDistributedDir failed with ' + error);
  }
})()

你可能感兴趣的鸿蒙文章

harmony 鸿蒙应用模型

harmony 鸿蒙使用显式Want启动应用组件

harmony 鸿蒙使用隐式Want打开网址

harmony 鸿蒙AbilityStage组件容器

harmony 鸿蒙访问DataAbility

harmony 鸿蒙FA模型访问Stage模型DataShareExtensionAbility

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙常见action与entities

harmony 鸿蒙API切换概述

harmony 鸿蒙app和deviceConfig的切换

0  赞