Basic knowledge of Hongmeng development learning introduction

Basic knowledge of Hongmeng development learning introduction

Hongmeng HarmonyOS development wiki

1, Concept of HAP

The software package of HarmonyOS is very interesting. It is called APP Pack. It is different from the APK of Android. Because of the distributed system, its software package is an overall concept, including multiple independent haps (this is equivalent to APK).

Like APK, HAP is a module package composed of code, resources, third-party libraries and application configuration files.

HAP has two module types: entry and feature. The former is the main entrance module. The latter is a separate business module.

2, Concept of Ability

It is equivalent to the four major components in Android (Activity, Service, ContentProvider and BroadcastReceiver).
Ability can be divided into two types: FA (Feature Ability) and PA (particle ability).

Ability also has three templates:
Page Ability
It is the only template supported by FA and provides user interaction ability.
Service Ability
The template supported by PA provides the ability to run tasks in the background.
Data Ability
PA supports templates that provide a unified data access abstraction.

When registering Ability in the configuration file (config.json), you can configure the "type" attribute in the Ability element
Specify the Ability template type, for example:

{
 	"module": {
 	...

	"abilities": [
 	{
	...
	"type": "page"
	...
	 }
	]
	 ...
 }


3, Page Ability

Page Ability is an overall concept. It does not refer to a single page, but may contain multiple pages. Page is the concept of module. Each page is represented by an instance of AbilitySlice, which refers to the sum of a single page of an application and its control logic.

Therefore, Page Ability is included in AbilitySlice. There is a high correlation between the two. In short, they are different interfaces of the same module. For example, the mall module includes the commodity details interface and the commodity list interface. You cannot put a user rights interface in the mall module.

Page module is not a simple concept, but a module with operating entities. Because it is not like the simple folder naming and package name distinguishing module in Android, the jump of the whole application is unified route maintenance. Therefore, when jumping to a separate interface, the page module will maintain a route to maintain the jump rules.

The default display Page of the Page module, AbilitySlice, is set by setMainRoute().
The exposed routing rules of AbilitySlice are used for other Page modules to jump in and set through AddActionroute().

public class MyAbility extends Ability {
	@Override
	public void onStart(Intent intent) {
 		super.onStart(intent);
 		
		// set the main route
		setMainRoute(MainSlice.class.getName());

		// set the action route
		addActionRoute("action.pay", PaySlice.class.getName());
		addActionRoute("action.scan", ScanSlice.class.getName());
	}
}

The action name used in addActionRoute() method needs to be registered in the application configuration file (config.json):

 {
	"module": {
	"abilities": [
		{
			"skills":[
				{
					"actions":[
						"action.pay",
						"action.scan"
					 ]
				 }
			]
			...
		 }
	 ]
	...
	}
 ...
}

3, Page Ability and AbilitySlice life cycle


Page lifecycle:
onStart()
When created for the first time, it will only be triggered once.
onActive()
Display the interaction status between the application and the user on the front desk.
onInactive()
When Page loses focus
onBackground()
When the Page is no longer visible to the user, in the background
onForeground()
When you return to the front desk
onStop()
When the system is about to destroy the Page

When the system creates a Page instance for the first time, the onStart() callback is triggered. For a Page instance, the callback is used during its life cycle
Page will enter the INACTIVE state after the logic is triggered only once. The developer must rewrite the method and configure it here
Set the AbilitySlice displayed by default.

@Override
public void onStart(Intent intent) {
	super.onStart(intent);
	super.setMainRoute(FooSlice.class.getName());
}

As an integral unit of a Page, the life cycle of an AbilitySlice depends on the life cycle of the Page to which it belongs.
Because the AbilitySlice carrier
The onStart() callback of AbilitySlice must be overridden and passed through setUIContent() in this method
Method setting page, as follows:

@Override
protected void onStart(Intent intent) {
	super.onStart(intent);

	setUIContent(ResourceTable.Layout_main_layout);
}

4, Route jump processing

The navigation between AbilitySlice is realized through the present()/presentForResult() method

@Override
protected void onStart(Intent intent) {

    ...
    Button button = ...;
    button.setClickedListener(listener -> present(new TargetSlice(), new Intent()));
    ...

}
int requestCode = positiveInteger; // Any positive integer.

@Override
protected void onStart(Intent intent) {

    ...
    Button button = ...;
    button.setClickedListener(
        listener -> presentForResult(new TargetSlice(), new Intent(), positiveInteger));
    ...

}

@Override
protected void onResult(int requestCode, Intent resultIntent) {
    if (requestCode == positiveInteger) {
        // Process resultIntent here.
    }
}

Construct the Operation object and realize the navigation between different pages through startAbility()/startAbilityForResult()

Requestor

Construct the Intent and the Operation object containing the Action in the Ability, and call the startAbilityForResult() method to initiate the request. Then rewrite the onAbilityResult() callback method to process the request result.

private void queryWeather() {
    Intent intent = new Intent();
    Operation operation = new Intent.OperationBuilder()
            .withAction(Intent.ACTION_QUERY_WEATHER)
            .build();
    intent.setOperation(operation);
    startAbilityForResult(intent, REQ_CODE_QUERY_WEATHER);
}

@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
    switch (requestCode) {
        case REQ_CODE_QUERY_WEATHER:
            // Do something with result.
            ...
            return;
        default:
            ...
    }
}

Processing party

As the object of processing requests, it is first necessary to declare the externally provided capabilities in the configuration file, so that the system can find itself and act as a candidate request handler

{
    "module": {
        ...
        "abilities": [
            {
                ...
                "skills":[
                    {
                        "actions":[
                            "ability.intent.QUERY_WEATHER"
                        ]
                    }
                ]
                ...
            }
        ]
        ...
    }
    ...
}

Configure the route in the Ability to navigate to the corresponding AbilitySlice with this action.

@Override
protected void onStart(Intent intent) {
    ...
    addActionRoute(Intent.ACTION_QUERY_WEATHER, DemoSlice.class.getName());
    ...
}

Process the request in the Ability and call the setResult() method to temporarily store the returned result.

@Override
protected void onActive() {
    ...
    Intent resultIntent = new Intent();
    setResult(0, resultIntent);   //0 is the resultCode returned after the current Ability is destroyed.
    ...
}

5, Selection of UI framework

At present, HarmonyOS has two UI development frameworks: Java and JS. Declarative programming is the current trend, and JS is recommended.

Tags: Java Javascript distributed system harmonyos

Posted by sprocket on Mon, 18 Apr 2022 07:58:32 +0300