In-depth use of Hongmeng Intermediate Ability (basic knowledge of HarmonyOS Hongmeng development)

Ability

Ability is an abstraction of the functions that HarmonyOS applications can provide. The capabilities of HarmonyOS applications are divided into two types: Feature Ability and Particle Ability. Feature Ability represents a function with a UI and is designed to interact with users. Particle Ability means that there is no UI capability, and it is mainly used to provide support for FA, for example, providing computing functions as background services or providing data access functions as data storage libraries. The two functions provide you with different templates for you to implement different functions. Currently, HarmonyOS provides the following types of capability templates:
Note: In the description below, page, service or data functions refer to functions using the corresponding template, while individual functions refer to functions using any type of template.

  • Page: Display the function of the UI. The UI is presented through AbilitySlice. You must override the onStart (ohos.aafwk.content.Intent) method and use the setMainRoute (java.lang.String) and addActionRoute (java.lang.String, java.lang.String) methods to configure the entry of the Page function.

  • Service: A function that runs in the background and has no UI. It is used to develop services that are always running in the background or connected to other functions. When the service capability is connected with other capabilities, a remote object will be returned, and you can use the remote object to call the function provided by the service capability.

  • Data: A function for manipulating data without UI. It provides methods for inserting, deleting, updating and querying data and opening files. You must implement these methods.


Introduction to Ability

Each application has a configuration file: config.json. The file is stored in the root directory of the Java code.

Screenshot 2020-09-22 3.13.14 in the afternoon.png

The description of the application file structure is as follows:

This entry stores the application code, resource files and configuration files. The folder name is editable.

entry / libs stores third-party library files. When creating a project, the IDE will automatically generate this directory.

entry / src / main / java is used for code development. You can change the file name in this directory as needed. When creating a project, the IDE will automatically generate this directory.

entry / src / main / resources / base / media stores PNG and JPG image files for your application. When creating a project, the IDE will automatically generate this directory.

entry / src / main / resources / base / element stores resource files used to read text resources. When creating a project, the IDE will automatically generate this directory.

The entry/src/main/config.json file is located in the main root directory. This file contains configuration information about the application. The system runs the application and displays the content on the UI based on the content of this file. When creating a project, the IDE will automatically generate this file.

To develop a new capability, you must register the capability in the config.json file. Sample code:

 {
     "module":{
         ...
         "abilities":[
           {
               ...
               "description": "Main ability of hiworld",
               "name": ".MainAbility",
               "label": "main ability",
               "icon": "main-ability.png",
               "type": "page",
               "visible": true,
               "orientation": "unspecified",
               "launch-mode": "standard",
               ...
           }
         ]
         ...
     }
 }
 

Note: All functions of the application must be registered in this file and attached to the function label.

  • The type tag indicates the type of template used by the capability, and its value page, service or provider indicates the page, service or data template respectively. The type label must be specified.
  • The name tag indicates the name of the capability and must be specified.
  • You can keep the default values ​​of the other labels.

Capability life cycle

As the basic unit of the application, the function has the following four life cycle states:

  • Initial: The ability is loaded into memory but not running. This is the initial state of all abilities.
  • Inactive: The function is loaded and executed, but it is not interactive. Usually, it is in an intermediate state before the capability changes to ACTIVE or Background. In this state, the UI may be visible, but cannot receive input events.
  • Activity: This feature is visible and interactive. Think that this ability has focus.
  • Background: This ability is invisible. If the system memory is insufficient, the function in this state will be destroyed first.

The figure below shows the complete capability life cycle. Each state transition will execute a specific callback. You can override the callback method.

image.png

Unauthorized reprinting is prohibited


For more technical exchanges, please join the QQ group

Group name: harmonyos Hongmeng Technology Exchange
Group number: 856567895


Note: All page functions must implement onStart (ohos.aafwk.content.Intent) to set its UI. To override the life cycle callback method, you must first call the callback method corresponding to the parent class, such as super.onStart(). State transition is implemented on the main thread. Therefore, it is recommended that you perform short logic in the life cycle callback to prevent the main thread of the function from being blocked.

 public class MainAbility extends Ability {
      protected void onStart(Intent intent);
 
      protected void onActive();
 
      protected void onInactive();
 
      protected void onForeground(Intent intent);
 
      protected void onBackground();
 
      protected void onStop();
  }
         Button button = new Button(this);
         button.setClickedListener(listener -> {
             Operation operation = new Intent.OperationBuilder()
                     .withDeviceId("")
                     .withBundleName("com.huawei.hiworld")
                     .withAbilityName("com.huawei.hiworld.MainAbility")
                     .build();
 
             Intent intent = new Intent();
             intent.setOperation(operation);
             intent.setParam("age", 10);
 
             startAbility(intent);
         });
 

For service functions, you can override the onConnect (ohos.aafwk.content.Intent) method to provide a RemoteObject to call. By default, null is returned.

     protected IRemoteObject onConnect(Intent intent) {
         return null;
     }

Unauthorized reprinting is prohibited


For more technical exchanges, please join the QQ group

Group name: harmonyos Hongmeng Technology Exchange
Group number: 856567895


Start from scratch to learn HarmonyOS Hongmeng 2.0 development

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/108734193