Harmonyos (Hongmeng), Java introduction to proficient electronic version

A geek spirit 2022-01-26 14:03:41 阅读数:997

harmonyos hongmeng java introduction proficient

HarmonyOS Application and tradition in App The application has a big bright spot ,HarmonyOS It provides a way to provide services for the future , Atomic Services .

Atomic services can be installed free ( There is no need to display the installation , It is very convenient for the system background to actively install on demand ), It supports running in 1+8+N On the device .

Atomic services and traditional App The application comparison is as follows :

| project | Atomic Services | Tradition App application |

| — | — | — |

| Package form | APP Pack(.app) | APP Pack(.app) |

| Distribution platform | By the atomic service platform (Huawei Ability Gallery) Manage and distribute | By the application market (AppGallery) Manage and distribute |

| Whether there is a desktop after installation icon | No desktop icon, But it can be added to the desktop manually , The display form is Service card | With desktop icon |

| HAP Package free installation requirements | all HAP package ( Include Entry HAP and Feature HAP) All need to meet Installation free requirement | all HAP package ( Include Entry HAP and Feature HAP) All are non installation free |

Official website address :

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/atomic-service-definition-0000001090840664

Atomic services are provided by HAP Package composition ,HAP yes Ability Deployment package for ,HarmonyOS Application code around Ability Component expansion . One HAP It's made up of code 、 resources 、 Third party library and application configuration file module package , Can be divided into entry and feature Two module types .

See the official website for details :

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-fundamentals-0000000000041611

An atomic service can have one or more HAP form , And one HAP Corresponding 1 individual FA perhaps 1 individual PA, Every FA or PA Can operate independently , complete 1 A specific function ;1 One or more functions ( Corresponding FA or PA) complete 1 A specific convenience service .

therefore HarmonyOS Of App The structural diagram of the installation package is as follows :

 Compositional relationships in atomic Services .png

3.3.2 The idea of separability and combination

according to 3.1 in HarmonyOS Of App The structure of the installation package is shown below , One HarmonyOS The installation package can be composed of one or more packages as needed HAP form , The composition here is optional , The application combination can be made according to the user's needs and hardware equipment .

For example, use HarmonyOS The TV and mobile phone of the system can have the following functions :

HarmonyOS The TV :

HarmonyOS video — The TV .png

HarmonyOS mobile phone :

HarmonyOS video — mobile phone .png

The TV has no function of SMS and call , But it has the function of controlling channel switching , Therefore, the control channel can be installed .HAP; The mobile phone has the function of SMS and call , Therefore, SMS can be installed .HAP And through the .HAP. Both have game functions , Therefore, you can install games .HAP. therefore HarmonyOS Of HAP Installation package , It can be installed reasonably according to the actual needs of users or hardware facilities , Realize the idea that the installation package can be separated and combined .

For different functions , It is recommended to write the same HAP in , Like text messages .HAP, This can reduce the time when users use a specific function , The complexity of downloading the installation package by the system or installing in the user field .

3.3.3 HAP Medium Ability

For the above game .HAP, For example, men go down a hundred floors , We will develop a Ability, It will have many sub functions , For example, login. 、 register 、 game 、 equipment 、 Chat 、 wait . These different functions can be integrated into different systems AbilitySlice in , One AbilitySlice Corresponding to a page .

game .HAP Of Ability With its AbilitySlice The diagram is as follows :

 game .HAP.png

Sign in 、 register 、 game 、 equipment 、 Chat 、 wait AbilitySlice Join one Ability in , Different AbilitySlice You can jump between , Finally packaged into a running game .HAP

Four 、 The configuration file Config.json understand

===================

4.1、 brief introduction


4.1.1 structure

Each application HAP There is one in the root directory of “config.json” The configuration file . The document mainly covers the following three aspects :

config.json.png

The following is a simple one HarmonyOS The application code structure seen after the application is created ,entry -> src -> main -> config.json file . The three modules are app、deviceConfig、module( The three pieces I'm showing here are the scaled content .)

image.png

4.1.2 Edit view

config.json Document adoption JSON File format , Each item consists of an attribute and a value . The attribute values are in no order , But it can't happen again , Must be unique . The value is JSON Basic data types for .HarmonyOS Of DevEco Studio Two types of editing are supported config.json The way , They are code editing view and visual editing view .

Code editing view :

image.png

Visual editing view :

image.png

4.2、 Profile details


The configuration file is created by app、deviceConfig、module Three parts , All three must exist . The following will be based on the above HarmonyOS The basis of HelloWorld Program analysis is one of the most basic config.json The configuration file , If you need a very detailed introduction , Please go straight to HarmonyOS The official website of , The following is the direct official website address :

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-config-file-elements-0000000000034463

4.2.1 app

app Object contains the global configuration information of the application ,HarmonyOS The basis of HelloWorld programmatic config.json In the configuration file app Part of it is as follows :

“app”: {

“bundleName”: “com.example.demo”,

“vendor”: “example”,

“version”: {

“code”: 1000000,

“name”: “1.0.0”

}

}

| The attribute name | Sub attribute name | meaning | data type | Can I default |

| — | — | — | — | — |

| bundleName | - | Represents the package name of the application , Used to identify the uniqueness of the application . | character string | no |

| vendor | - | Represents a description of the application developer . The string length does not exceed 255 byte . | character string | It can be defaulted , The default value is empty . |

| version | - | Represents the version information of the application . | object | no |

| | code | Indicates the version number of the application , Only used for HarmonyOS Manage the app , Do not render to the end user of the application . | The number | no | The number | no |

| | name | Indicates the version number of the application , Used to present... To the end user of the application . Less than 127 Bytes . | character string | no |

4.2.2 deviceConfig

deviceConfig Application configuration information contained on the specific device , Can contain default、phone、tablet、tv、car、wearable、liteWearable and smartVision Equal attribute .default The configuration in the label is common to all devices , Other equipment types with special requirements , You need to configure it under the label of the device type .HarmonyOS The basis of HelloWorld programmatic config.json In the configuration file deviceConfig Part of it is as follows :

“deviceConfig”: {}

4.2.3 module

module Object contains HAP Package configuration information , Include each Ability Basic properties that must be defined ( Such as package name 、 Class name 、 Type and Ability Capabilities provided ), As well as the permissions required for the application to access the system or other protected parts of the application .HarmonyOS The basis of HelloWorld programmatic config.json In the configuration file module Part of it is as follows :

“module”: {

“package”: “com.example.demo”,

“name”: “.MyApplication”,

“mainAbility”: “com.example.demo.MainAbility”,

“deviceType”: [

“phone”

],

“distro”: {

“deliveryWithInstall”: true,

“moduleName”: “entry”,

“moduleType”: “entry”,

“installationFree”: true

},

“abilities”: [

{

“skills”: [

{

“entities”: [

“entity.system.home”

],

“actions”: [

“action.system.home”

]

}

],

“orientation”: “unspecified”,

“name”: “com.example.demo.MainAbility”,

“icon”: “$media:icon”,

“description”: “$string:mainability_description”,

“label”:

《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing

“$string:entry_MainAbility”,

“type”: “page”,

“launchType”: “standard”

}

]

}

| The attribute name | Sub attribute name | meaning | data type | Can I default |

| — | — | — | — | — |

| package | - | Express HAP Package structure name for , Uniqueness should be guaranteed in the application . Using reverse domain name format ( Recommendations and HAP The project catalog of the project is consistent ). The string length does not exceed 127 byte . | character string | no |

| name | - | Express HAP The name of the class . Using reverse domain name to express , The prefix needs to be of the same level package The package name specified by the tag is consistent , Also can use “.” The way the beginning is named . The string length does not exceed 255 byte . | character string | no |

| mainAbility | - | Express HAP The entrance to the bag ability name . | character string | page Type of Ability Why not |

| deviceType | - | It means to allow Ability Type of equipment running . The predefined device types of the system include :phone( mobile phone )、tablet( Flat )、tv( Wisdom screen )、car( Car engine )、wearable( Smart wear )、liteWearable( Lightweight smart wear ) etc. . | Array of strings | no |

| distro | - | Express HAP Specific description of the release | object | no |

| |

deliveryWithInstall

|

At present HAP Whether to support installation with application .

  • true: Support installation with application .

  • false: Install with app is not supported

|

Boolean type

|

no

|

| |

moduleName

|

HAP The name of

|

character string

|

no

|

| |

moduleType

|

At present HAP The type of , There are two types :entry and feature

|

character string

|

no

|

| |

installationFree

|

Indicates the current FA Whether the installation free feature is supported .

  • true: Indicates that the installation free feature is supported , And comply with installation free constraints .

  • false: Indicates that the installation free feature is not supported .

|

Boolean type

|

entry.hap It can be defaulted ,feature.hap You can't default .

|

|

abilities

|

-

|

Represents all... In the current module Ability. Using object array format , Each of these elements represents a Ability object .

|

An array of objects

|

yes

|

| |

skills

|

Express Ability Be able to receive Intent Characteristics of .

|

An array of objects

|

yes

|

| |

orientation

|

It means that we should Ability Display mode of . This label only applies to page Type of Ability. The range of values is as follows :

  • unspecified: The display direction is automatically determined by the system .

  • landscape: Screen mode .

  • portrait: Vertical screen mode .

  • followRecent: Follow the latest application in the stack .

|

character string

|

yes

|

| |

name

|

Express Ability name . The value can be expressed in reverse domain name , It consists of package name and class name , Such as “com.example.myapplication.MainAbility”; Also can use “.” The beginning class name means , Such as “.MainAbility”. This label only applies to mobile phones 、 Flat 、 Wisdom screen 、 Car engine 、 Smart wear .

|

character string

|

no

|

| |

icon

|

Express Ability Index of icon resource file . Examples of values :$media:ability_icon. If in the Ability Of “skills” Properties of the ,“actions” The value of contains  “action.system.home”,“entities” The value contains “entity.system.home”, Then Ability Of icon Will also be used as an application of icon. If there are more than one qualified Ability, Take the one at the front Ability Of icon As an application icon.

|

character string

|

yes

|

| |

description

|

Said to Ability Description of . The value can be descriptive , It can also be a resource index of descriptive content , To support multilingualism .

|

character string

|

yes

|

| |

label

|

Express Ability The name displayed to the user . The value can be Ability name , It can also be a resource index of the name , To support multilingualism .

|

character string

|

yes

|

| |

type

|

Express Ability The type of . The range of values is as follows :

  • page: Based on Page Template development FA, Used to provide the ability to interact with users .

  • service: Based on Service Template development PA, Used to provide the ability to run tasks in the background .

  • data: Based on Data Template development PA, Used to provide a unified data access abstraction to the external world .

  • CA: Indicates that it supports other applications to call the... In the form of a window Ability.

|

character string

|

no

|

| |

launchType

|

Express Ability Start mode of , Support “standard”、“singleMission” and “singleton” Three models :

  • standard: It means that we should Ability There can be many instances .“standard” Patterns apply to most application scenarios .

  • singleMission: It means this Ability There can only be one instance in each task stack .

  • singleton: It means that we should Ability There can be only one instance in all task stacks . for example , The call interface with global uniqueness adopts “singleton” Pattern .

This label only applies to mobile phones 、 Flat 、 Wisdom screen 、 Car engine 、 Smart wear .

|

character string

|

no

|

abilities -> skills

| The attribute name | Sub attribute name | meaning | data type | Can I default |

| — | — | — | — | — |

| skills | - | Express Ability Be able to receive Intent Characteristics of | An array of objects | yes |

| | entities | To be able to receive Intent Of Ability Categories ( Video frequency 、 Desktop applications, etc ), Can contain one or more entity. | Array of strings | yes |

| | actions | To be able to receive Intent Of action value , Can contain one or more action. | Array of strings | yes |

5、 ... and 、HarmonyOS The running process of the application

====================

5.1、 Get ready


5.1.1 Create project

Get into HarmonyOS( Hongmeng ) Before formal development , It can be done with a simple HelloWorld To understand the relevant processes of its runtime . Here is the passage DevEco Studio Create a simple HelloWorld Sample code structure , Next, we will analyze the startup process .

image.png

5.1.2 Run the project

Run the top HelloWorld, The display effect is as follows :

image.png

5.2、 Analyze and start the process


5.2.1 Start flow chart

A simple HarmonyOS( Hongmeng )HelloWorld Program , Its basic operation process , It can be divided into the following stages :

 Start process .png

5.2.2 analysis config.json file

config.json File is HarmonyOS( Hongmeng ) The main configuration file of the application , It is located in entry -> src -> main -> config.json, The configuration file is divided into three parts , For specific instructions, please refer to another document of mine ,《HarmonyOS( Hongmeng )——config.json Detailed explanation 》

image.png

5.2.3  initialization

Initialization is mainly through config.json Of module Under the package and name To locate the class com.example.demo.MyApplication.

image.png

MyApplication Is as follows , It will do some application initialization

package com.example.demo;

import ohos.aafwk.ability.AbilityPackage;

public class MyApplication extends AbilityPackage {

@Override

public void onInitialize() {

super.onInitialize();

}

}

5.2.4 Gain entry Ability Full class name

entrance Ability Full class name of , Also in the config.json File to configure

image.png

MainAbility Corresponding config.json Medium abilities Relevant configuration information in

image.png

MainAbility The main contents are as follows , Sub interfaces will be loaded in the main interface , adopt super.setMainRoute(MainAbilitySlice.class.getName()); The way to load , About the relevant introduction of the page, you can see another document of mine 《HarmonyOS( Hongmeng )—— Ability And page 》

1package com.example.demo;

2

3import com.example.demo.slice.MainAbilitySlice;

4import ohos.aafwk.ability.Ability;

5import ohos.aafwk.content.Intent;

6

7public class MainAbility extends Ability {

8    @Override

9    public void onStart(Intent intent) {

10        super.onStart(intent);

11        super.setMainRoute(MainAbilitySlice.class.getName());

12    }

13}

5.2.5 function Ability

This step is to start the above class

5.2.6 function Ability Sub interface of

MainAbility After running onStart Method will load the sub page , The sub page here is MainAbilitySlice, The main contents are as follows .

package com.example.demo.slice;

import com.example.demo.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

public class MainAbilitySlice extends AbilitySlice {

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

}

stay MainAbilitySlice We found that ,onStart Load class related UI To configure ,ResourceTable.Layout_ability_main,ResourceTable yes HarmonyOS Constant classes generated dynamically when the application starts , It will give everyone xml Wait for the file to assign a flag ID. If you want to see this class , You can run the application in class Refer to... In the document

image.png

5.2.7 load xml file , Show content

About the content in the sub page , You can go through xml File to configure , This is based on xml The way to configure content styles is also more convenient , Easy for unified management and system analysis .

image.png

At this point HarmonyOS( Hongmeng )HelloWorld There are so many execution processes of the program .

6、 ... and 、 Selected articles

======

HarmonyOS( Hongmeng )DevEco Studio Development environment construction

HarmonyOS( Hongmeng ) Introduction to development

Two cases are easy to get started in five minutes Harmony( Hongmeng ) Development

copyright:author[A geek spirit],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261403395996.html