Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

OS Application Development practice method course

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the "OS Application Development practice method course". In the daily operation, I believe that many people have doubts about the OS application development practice method tutorial. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "OS Application Development practice method tutorial". Next, please follow the editor to study!

(1) Ability

Before we begin, let's take a look at a basic concept: Ability

Ability literally means "ability" in English, and also in Hongmeng's architecture, the official interpretation: Ability means that the abstraction of the capabilities of the application is an important part of the application. An application can have multiple capabilities (that is, it can contain multiple Ability), and HarmonyOS supports applications to be deployed in Ability units. Ability can be divided into two types: FA (Feature Ability) and PA (Particle Ability). Each type provides developers with different templates in order to achieve different business functions.

Ability supports three templates:

PageAbility: a page template that provides the ability to interact with users. This is also the only template supported by FA.

ServiceAbility: a service template that provides the ability to run tasks in the background.

DataAbility: a data template used to provide a unified data access abstraction to the outside world.

To put it simply, if you want to do UI interaction, use FA. Use PA if you need to do non-interface service / data processing.

The hierarchy can be seen in the following figure:

When you register Ability in the configuration file (config.json), you can specify the type of current Ability template by configuring the type attribute in the Ability element:

The screenshot above specifies a page, indicating that this is an instance of a page template.

Similarly, if you are creating a PA, you can also specify service or data.

Whether service or data, is an ability, the difference is how developers define the responsibilities of this class (whether to provide service / data support, or as an interface interaction?), in short, it depends on what capabilities you want the ability you create to provide you.

(2) Page jump

Let's talk about PageAbility in detail. Page template is the only template supported by FA and provides the ability to interact with users (UI). A Page can be composed of one or more AbilitySlice. When we ran hello world before, we introduced that Ability is a routing portal, and AbilitySlice is a place to write interaction logic.

To put it simply, when we create a FA, such as this TestAbility, we automatically create a TestAbilitySlice,TestAbility equivalent to a class that can be called externally, and the specific logic implementation is in TestAbilitySlice. When TestAbility is called, it automatically maps the route to TestAbilitySlice to provide specific interaction logic.

By default, a FeatureAbility specifies a default route, that is, its corresponding AbilitySlice is specified by the setMainRoute method. Of course, we can also modify the default assignment by the addActionRoute method. Examples of specific methods are as follows:

1. Add actions to the config.json file:

two。 In the corresponding Ablity, use the addActionRounte method to add routes in onStart.

AddActionRoute ("action.test", TestAbilitySlice.class.getName ())

3. Call:

Private void TestAction () {Intent intent = new Intent (); Operation operation = new Intent.OperationBuilder (). WithAction ("action.test") .build (); intent.setOperation (operation); startAbility (intent);}

Here is a more detailed explanation of the Intent:

Intent is the carrier for transmitting information between objects. For example, when an Ability needs to start another Ability, or when an AbilitySlice needs to navigate to another AbilitySlice, you can specify the startup target through Intent to carry the relevant data. As mentioned in the previous article, it is a middleware.

This enables inter-Page access to this AbilitySlice. There are two cases of page jump: jump within Page (Ability) and jump between Page. Of course, in either case, you need to jump with the help of middleware Intent, and Intent also supports carrying parameters to transfer data parameters between Page or between Ability.

(1) Jump within Page

When you jump within the same Page, that is, when the AbilitySlice that initiates the jump and the AbilitySlice of the jump target are in the same Page, you can use the present () method to jump.

If the jump needs to return a result, you can use the presentForResult () method to achieve the jump. When the user returns from the jump target, the system will call back onResult () to receive and process the returned results. The method needs to be overridden at this point. The return result is set by the jump target AbilitySlice through the setResult method during its life cycle.

(2) Jump between Page

The AbilitySlice in different Page is not visible to each other, so you cannot jump directly to the AbilitySlice of another Page through present or presentForResult methods. However, you can navigate to the target's AbilitySlice by configuring the Action of the Intent. Navigation between Page can be switched using startAbility or startAbilityForResult method. A similar startAbilityForResult method has an onAbilityResult method to get a callback of the returned result. Set the return result in Ability by using the setResult method, see the section above that adds action.

(3) UI framework

Going back to the UI framework (JAVA), we can create UI structures in two ways: Java code and XML.

1. Use the code to build the UI interface steps:

(1) define the layout:

DirectionalLayout layout = new DirectionalLayout (this)

(2) scheduled layout configuration:

LayoutConfig config = new LayoutConfig (LayoutConfig.MATH_PARENT,LayoutConfig.MATH_PARENT)

(3) configure the layout:

Layout.setLayoutConfig (config)

(4) configure the layout background:

ShapeElement element = new ShapeElement (); element.setRgbColor (new RgbColor (255255255)); layout.setBackground (element)

(5) add components to the layout (here take the text as an example):

Text text = new Text (this); text.setLayoutConfig (config); text.setText ("Hello, Hong Meng"); text.setTextColor (new Color (0xFF000000)); text.setTextSize (50); text.setTextAligment (TextAligment.CENTER); layout.addComponent (text)

(6) set UI content

Super.setUIContent (layout)

With these captures, you can create the interface in code form.

two。 Use XML to build the page:

(1) build a XML file: right-click in the base directory to create a layout file:

(2) after the creation is completed, the corresponding XML file will be generated in the base- > layout directory. The initial content is as follows:

(3) create component elements:

Xmlns:ohos= "http://schemas.huawei.com/res/ohos" ohos:height=" match_parent "ohos:width=" match_parent "ohos:orientation=" vertical "> ohos:height=" match_content "ohos:width=" match_content "ohos:text=" Hello, Hongmeng "/ >

There are many other parameters under the Text tag, most of the same components have similar or similar tag setting parameters, you can enter ohos automatic completion to try, basically can be directly understood by the literal meaning of the use, here will not repeat.

(4) use resource mapping ResourceTable to set UI content:

Super.setUIContent (ResourceTable.Layout_mytest); at this point, the study on the "practical method course of OS Application Development" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report