HCIA-HarmonyOS Application Developer study notes

1. Introduction to HarmonyOS

HarmonyOS is a brand-new distributed operating system for the Internet of Everything era. It is a full-scenario distributed smart operating system covering 1+8+N full-scenario terminal devices, in which mobile phones are the core of the entire distributed capability.
Please add image description

Compared with the native Android system, HarmonyOS can not only support mobile phones, but also computers, smart screens, wearable devices, smart homes, car systems, etc., thus forming a unified operating system for all scenarios.

Through a variety of distributed technologies, different terminal hardware capabilities are integrated to form a virtual "hyper terminal". Application developers can develop applications based on this "hyper terminal" and focus on upper-layer business logic. The advantage is that there is no need to pay attention to hardware differences; device development Users can call other terminal capabilities on demand, bringing an innovative service experience based on "Super Terminal".

2. HarmonyOS application development process

HarmonyOS system architecture

HarmonyOS as a whole can be divided into 内核层, , 系统服务层, 应用框架层and 应用层.

The system service layer is the core capability set of HarmonyOS. It provides services to applications through the framework layer, such as distributed data management, distributed task scheduling, distributed soft bus, etc., all belong to the system service layer, while the Ability framework, for example, belongs to the application framework layer. . In addition, the application framework layer also includes UI framework, user program framework, etc.

HarmonyOS subsystem set

The set of subsystems of HarmonyOS can be divided into the following:

子系统集
系统基本能力子系统集
基础软件服务子系统集
增强软件服务子系统集
硬件服务子系统集

1. Common ones 系统基本能力子系统集: UI framework, Ability framework, user program framework, distributed task scheduling, distributed data management, distributed soft bus, etc.

2. Common ones 基础软件服务子系统集: multimedia subsystem, telephone subsystem, event notification subsystem, etc.

3. Common ones 增强软件服务子系统集: smart screen proprietary business subsystem, wearable proprietary business subsystem, IOT proprietary business subsystem, etc.

4. Common ones 硬件服务子系统集: location service subsystem, biometric identification service subsystem, wearable proprietary hardware service subsystem, IoT proprietary hardware service subsystem, etc.

DevEco Studio

DevEco Studio is based on the open source version of IntelliJ IDEA Community. It is a one-stop integrated development environment (IDE) for Huawei terminals in all scenarios and multiple devices. It provides developers with E2E HarmonyOS application development such as project template creation, development, compilation, debugging, and release. Service, which supports code development and debugging in Java, JavaScript, XML, HML(HarmonyOS Markup Language), C/C++, , language.CSS

Open DevEco Studio and create an application project. You can see the following directories and files in the generated basic application package structure:
Insert image description here
Insert image description here

When using the DevEco Studio tool for application development, it supports 预览器three 模拟器debugging 真机methods:

HarmonyOS application package structure

A HarmonyOS application package structure mainly includes HarmonyOS Ability Package APPand HAP(HarmonyOS Ability Package). An APP can contain multiple HAPs. It is a module package composed of 逻辑代码, 资源文件, 第三方库and and can be divided into two module types : and .应用配置文件EntryFeature

Among them, Entry.hapis the main entrance of the application, and Feature.hapis other parts of the application. Normally, there can only be one Entry.hap in an APP, which pack.infodescribes the relevant content of the application. In addition, at the root of each HAP There is a config.jsonconfiguration file in the directory .
Insert image description here
Here we only introduce the three main parts of the config.json configuration file:

HAP
app
deviceConfig
module

As follows, you can see the code:
Insert image description here

  • app contains the global configuration information of the application, the application's package name, version number, etc.; deviceConfig contains the configuration information of the application on specific devices; module contains the configuration information of each HAP package, with basic attributes defined by each Ability, including Package name, class name, capabilities provided by the Ability, etc.

Insert image description here

How to use resource files

Resource files in the base directory and qualifier directory can be referenced by specifying the resource type and resource name.
1. The format of resource files referenced in java files: ResourceTable.type_name;
2. The format of resource files referenced in XML files: $type:name;
3. Resource files in the rawfile directory are referenced by specifying the file path and file name.

authority management

Application permissions need to use the "reqPermissions" attribute in config.json to declare the required permissions one by one. If the relevant permissions are not declared in this file, the application will not be authorized for the permissions. In addition, the permissions used in the application Third-party libraries also involve permission usage and need to be declared in config.json.

Distributed capabilities

1.: 分布式软总线It is the basis of distributed capabilities and provides unified distributed communication capabilities for the interconnection of different devices.
2.: 分布式文件服务Provide file sharing capabilities for applications on different devices of users;
3.: 分布式数据服务Realize distributed management of application data and user data.
4.: 分布式任务调度Support operations such as remote startup, remote calling, remote connection, and migration of cross-device applications;
5.: 分布式设备虚拟化Provide resource integration, device management, and data processing between different devices for users, thereby forming a virtual hyper terminal;

3. Ability design and development

The concept and classification of Ability

Ability is the main component of the application, which can be divided into Feature Ability(FA)FA Particle Ability(PAsupport Page 模板, which is used to provide the ability to interact with users; PA support Service 模板, Data 模板Service template is used to provide the ability to run tasks in the background, and Data template is used to provide external Unified data access interface.

Ability
Feature Ability
Page Ability
Particle Ability
Service Ability
Data Ability

A Page Ability can be composed of one or more AbilitySlices, AbilitySlicewhich refers to the sum of a single page of the application and the control logic. In addition, different Page Abilities can jump between each other, and can be specified to jump to a specific AbilitySlice in the target Page Ability. .

Page Ability
AbilitySlice
AbilitySlice
......

Page life cycle

当用户进入、浏览、退出页面时,页面会有不同的状态,并回调不同的生命周期状态方法给外界,Page Ability 和AbilitySlice 具有类似的生命周期回调方法:
Insert image description here
1、onStart()方法:当系统首次创建Page实例时,触发该回调;对于一个Page实例,该回调在其生命周期过程中只触发一次,然后该逻辑进入INACTIVE状态。
2、onActive()方法:Page会在进入INACTIVE状态后来到前台。然后系统调用该回调,逻辑会进入ACTIVE状态,该状态是应用与用户交互的状态,此时Page将保持在该状态,除非某类事件发生导致Page失去焦点,比如用户点击返回键或导航到其他Page。
3、onInActive()方法:Page失去焦点后,系统将调用该回调,然后Page进入INACTIVE状态。
4、onBackground()方法:Page将不再对用户可见,该回调通知开发者进行相应的资源释放,然后Page进入BACKGROUND状态。
5、onStop()方法:系统要销毁Page时,该回调通知开发者进行系统资源的释放。
6、onForeground()方法:处于该状态的Page仍然驻留在内存中,当重新回到前台时,系统将调用该回调。

Intent载体

Intent 是对象之间传递信息的载体,它由OperationParameters 元素构成。其中,Action 表示动作,可自定义Action;Entity 表示类别,也可以自定义;Uri 表示Uri 的相关描述,用于匹配Uri 的信息;Flags 表示处理Intent 的方式;BundleName 表示包描述;AbilityName 表示待启动的Ability名称;Deviceld 表示运行指定Ability的设备ID。

通过Intent 启动应用的过程是:构造包含BundleName 与AbilityName 的Operation对象,从而启动一个Ability ,并导航到该Ability。

页面间导航

1、在同一Page内导航,使用present()presentForResult() 方法实现同一Page内两个AbilitySlice 间的跳转。
2、在不同Page间导航,通过配置Intent 的Action,并使用startAbility()startAbilityForResult() 方法导航到目标Ability。获得返回结果的回调为onAbilityResult() ,在Ability 中调用setResult() 可以设置返回结果。

Particle Ability 开发

1、Service Ability:主要用于后台运行任务,它在主线程中执行,可由其他应用或Ability启动。在一个设备上,同一个Service只存在一个实例,即Service Ability是单实例的。

  • 启动Service。通过将Intent传递给startAbility()方法来启动Service,不仅支持启动本地Service,还支持启动远程Service。另外通过构造包含Deviceld(设备ID)、BundleName(包名称)、AbilityName(待启动的Ability名称)的Operation对象来设置目标Service信息。
  • 连接Service。如果Service需要与Page Ability或其他应用的Service Ability进行交互,则应创建用于连接的Connection,另外,Service还支持其他Ability通过connectAbility()方法进行连接。在使用connectAbility()处理回调时,需要传入目标Service的Intent与IAbilityConnection的实例,其中IAbilityConnection提供了两个方法供开发者实现,onAbilityConnectDone()用来处理连接的回调,onAbilityDisconnectDone()用来处理断开连接的回调。
  • 停止Service。Service一旦创建就会一直保持在后台运行,除非必须回收内存资源,否则系统不会停止或销毁Service。要手动停止Service,有以下方法:在Service中通过terminateAbility()停止本Service;在其他Ability调用stopAbility()来停止Service。

2、Data Ability:使用Data模板的Ability,对外提供一些增删改查以及文件操作等接口,其具体实现由开发者提供。Data的提供方和使用方都通过URl来标识一个具体的数据,它分为两种场景,跨设备场景需要额外制定DeviceID,而本地场景不需要。

四、UI 页面设计与开发

Java UI 框架

1、组件布局
Java UI框架中提供了一些标准布局功能的容器,继承自ComponentContainer,一般以"Layout”结尾,StackLayout 布局方式是堆叠(层叠)布局,视图以层叠的方式显示,默认情况下放到这块区域的左上角,第一个添加到布局中的视图显示在最底层,最后一个被放在最顶层;DependentLayout 布局方式可以指定相对于其他同级元素或相对于父组件的位置;DirectionalLayout 布局方式它用于将一组组件按照水平方向(horizontal)或者垂直方向(vertical)排布,子组件可以通过权重来按比例占用父组件的大小;TableLayout 布局方式以使用表格的方式划分子组件,如下图:
Insert image description here
2、组件的分类
Java Ul框架的组件中,可以根据组件的功能从而分为布局类、交互类和显示类三种,布局类提供了不同布局规则的组件,交互类提供了在具体场景下与用户交互响应的功能,显示类提供了内容显示。

常用的组件有很多,例如,Text 是显示字符串的组件,定义后显示一块文本区域;TextField 是一种文本输入框组件;Button 是按钮组件,可以由文本、图标组成;Image 是显示图片的组件;ScrollView 是带滚动功能的组件,可以通过滚动显示更多内容。

JavaScript UI 框架

JavaScript UI 中基本组件主要有以下四类:

组件类型 主要组件
基础组件 text、image、progress、rating、span、marquee、image-animator、divider、search、menu、chart
容器组件 div、list、list-item、stack、swiper、tabs、tab-bar、tab-content、list-item- group、refresh、dialog、grid-container
媒体组件 video
画布组件 canvas

5. Task dispatch method

HarmonyOS provides developers with four task dispatch methods: synchronous task dispatching, asynchronous task dispatching, asynchronous delayed task dispatching and group dispatching tasks.

The TaskDispatcher framework provides the following main task dispatching methods:
①For delayDispatch(), 异步延迟派发任务asynchronous execution, the function returns immediately, and the task will be dispatched to the response queue internally after a specified delay;
②For syncDispatch(), 同步派发任务dispatch the task and wait for the task in the current thread The execution is completed. If used improperly, it will lead to deadlock;
asyncGroupDispatch()is 异步成组派发任务, and there is a certain connection between the group of tasks;
asyncDispatch()is 异步派发任务, dispatch the task and return immediately. The return value is an interface that can be used to cancel the task.

6. Task Distributor

HarmonyOS provides developers with the following four task dispatchers:
TaskDispatcher has multiple implementations, each implementation corresponds to a different task dispatcher, which can be divided into 全局并发任务分发器(GlobalTaskDispatcher), 并发任务分发器(ParallelTaskDispatcher), 串行任务分发器(SerialTaskDispatcher)and 专有任务分发器(SpecTaskDispatcher).

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/128914184