HarmonyOS/OpenHarmony application development

HarmonyOS: HarmonyOS is an operating system officially released by Huawei on August 9, 2019. It is a distributed operating system oriented to all scenarios, creating a super virtual terminal interconnected world, organically connecting people, equipment, and scenarios. On the basis of traditional single-device system capabilities, HarmonyOS proposes a distributed concept based on the same set of system capabilities and adapting to multiple terminal forms, which can support multiple terminal devices.

For consumers, HarmonyOS can integrate the capabilities of various terminals in life scenarios to form a "super virtual terminal", which can realize fast connection, mutual assistance, and resource sharing between different terminal devices, match suitable devices, and provide a smooth full-scenario experience. For application developers, HarmonyOS adopts a variety of distributed technologies, so that the development and implementation of applications has nothing to do with the differences in the form of different terminal devices, reducing the difficulty and cost of development. This allows developers to focus on upper-level business logic and develop applications more conveniently and efficiently. For device developers, HarmonyOS adopts a component-based design scheme, which can be flexibly tailored according to the resource capabilities and business characteristics of the device to meet the operating system requirements of different types of terminal devices.

OpenHarmony: OpenHarmony is an operating system developed by Huawei, mainly for IoT applications and smart devices. It adopts the microkernel architecture and is characterized by high efficiency, security and stability. OpenHarmony provides a development framework and tools that support development in multiple languages ​​such as JavaScript, Java, and C/C++. Developers can use the development tools and components provided by OpenHarmony, such as ArkUI, ArkJS, ArkCompiler, etc., to build services such as user interface, application modular development, and data storage.

Application development: Application development refers to the process of taking one or more applications from concept to actual operation using programming languages, tools, and frameworks. Application development typically includes the following steps:

  1. Determine requirements: Before application development, it is necessary to clarify the requirements of the application, including functional requirements, performance requirements, security requirements, etc.
  2. Design: According to the requirements, design the application program, including architecture design, interface design, data structure design, etc.
  3. Coding: Coding of the application using a programming language according to design documents and specifications.
  4. Testing: After coding is done, testing is required to ensure that the application works properly and conforms to the requirements and design.
  5. Deployment: Deploying an application to a target device or platform, making it available to users.
  6. Maintenance: After the application goes live, it needs to be maintained, including version updates, bug fixes, performance optimization, etc.

Application development can use different programming languages ​​and development frameworks, such as Java, Python, JavaScript, etc. At the same time, application development also needs to consider factors such as user experience, performance optimization, and security to ensure the quality and reliability of the application. Each language and framework has its advantages and use cases. At the same time, the quality and reliability of an application depends not only on the programming language and development framework, but also on factors such as user experience, performance optimization, and security. During the application development process, constant iteration and optimization are required to ensure that the final delivered application can meet the needs of users, provide a good user experience, and have stability and security.

HarmonyOS/OpenHarmony is an operating system launched by Huawei, which is mainly used for IoT applications and smart devices. Different from Android, HarmonyOS adopts microkernel architecture, which makes it more efficient, safer and more stable.

In terms of HarmonyOS/OpenHarmony application development, Huawei provides development frameworks and tools, and developers can use JavaScript, Java, C/C++ and other languages ​​for development.

JavaScript, Java, and C/C++ are three different programming languages, each with different characteristics and application scenarios.

  1. JavaScript: JavaScript is a scripting language that is mainly used in web development. It is usually used in front-end development to handle user interaction, form validation, page dynamics, etc. JavaScript can also be used for backend development, such as server-side programming with Node.js. JavaScript is a scripting language primarily used in web development. It is usually used in front-end development to handle user interaction, form validation, page dynamics, etc. JavaScript can also be used for backend development, such as server-side programming with Node.js. JavaScript is an interpreted language that can run directly in the browser without prior compilation. Thanks to its easy-to-learn syntax and rich API, JavaScript has become one of the most used languages ​​for web development. It can be used not only for web development, but also for mobile application development, game development, and more.

The application scenarios of JavaScript are very wide, and the following are some of them:

  1. Front-end development: JavaScript is usually used to handle user interaction, form validation, page dynamics, etc. It can make Web pages more dynamic and interactive, and improve user experience.
  2. Server-side development: With Node.js, JavaScript can also be used for server-side development. In this way, the front-end and back-end codes can be shared, reducing the workload of development and maintenance.
  3. Mobile App Development: Using JavaScript and related frameworks such as React Native and Ionic, it is possible to develop cross-platform mobile applications.
  4. Game development: 2D and 3D games can be developed using JavaScript and related game engines such as Phaser and Unity.

  1. Java: Java is an object-oriented programming language that is platform independent and can run on different operating systems and hardware. It is widely used in enterprise application development, mobile application development, game development and other fields.

Java is an object-oriented programming language that is platform independent and can run on different operating systems and hardware. It is widely used in enterprise application development, mobile application development, game development and other fields.

Features of Java include:

  1. Platform independence: Java's virtual machine (JVM) enables Java code to run on any platform that supports the JVM, without the need for separate compilation for each platform.
  2. Object-oriented: Java is an object-oriented language that supports object-oriented programming concepts such as encapsulation, inheritance, and polymorphism, which makes the code more modular and extensible.
  3. Security: Java has built-in security mechanisms to prevent code injection, buffer overflow and other security issues.
  4. Rich libraries and frameworks: Java has rich libraries and frameworks that can speed up development and improve code quality, such as Spring, Hibernate, Android SDK, etc.
  5. Excellent performance: Although Java is an interpreted language, its JIT (just-in-time compilation) technology can compile hot code into local machine code, thereby achieving performance close to C++.

The application scenarios of Java are very wide, and the following are some of them:

  1. Enterprise-level application development: Java is widely used in enterprise-level application development, such as e-commerce, finance, logistics and other fields.
  2. Mobile application development: Using Java and Android SDK, you can develop mobile applications for the Android platform.
  3. Game development: 2D and 3D games can be developed using Java and game engines such as LibGDX and jMonkeyEngine.
  4. Web Development: Web applications can be developed using Java and related frameworks such as Spring and Struts.

  1. C/C++: C and C++ are compiled languages ​​with efficient performance and direct access to the underlying hardware. They are widely used in system-level programming, game development, embedded systems and other fields. C++ is an extension of C that introduces the concept of object-oriented programming.

The C language has the following characteristics:

  1. Low-level language: C language can directly manipulate memory and hardware, so it can efficiently handle binary data and underlying hardware.
  2. Structured language: C language is structured, with only one entry point, the main function. In a program, all code must be placed inside functions.
  3. Statically typed language: C language is a statically typed language, which needs to determine the type of variables at compile time.
  4. Weakly typed language: The type checking of C language is relatively loose, and it is easy to be troubled by problems such as overflow and cross-border.
  5. Efficient code: Since the C language can directly manipulate memory and hardware, the generated code is very efficient.

The application scenarios of C language are very wide, the following are some of them:

The application scenarios of C++ language are very wide, the following are some of them:

  1. System programming: C language is the main programming language for system-level software such as operating systems, drivers, and compilers.
  2. Embedded system: C language is also the main programming language for embedded systems, such as smart home, smart wearable devices, etc.
  3. Game development: C language is also widely used in game development, such as game engines, physics engines, etc.
  4. Network programming: C language is widely used in network programming, such as TCP/IP protocol stack, router protocol, etc.

    The C++ language has the following characteristics:

  5. Low-level language: The C++ language can directly manipulate memory and hardware, so it can efficiently handle binary data and underlying hardware.
  6. Structured language: The C++ language is structured, with only one entry point, the main function. In a program, all code must be placed inside functions.
  7. Statically typed language: The C++ language is a statically typed language, which requires the type of variables to be determined at compile time.
  8. Strongly typed language: The type checking of C++ language is relatively strict, which can effectively reduce errors.
  9. Object-oriented: The C++ language supports object-oriented programming, with features such as classes, objects, inheritance, and polymorphism, which can improve code reusability and maintainability.
  10. STL: The C++ language standard library provides a large number of containers, algorithms and other components, which can facilitate the realization of data structures and algorithms.
  11. System programming: C++ language is one of the main programming languages ​​for system-level software such as operating systems, drivers, and compilers.
  12. Game development: C++ language is also widely used in game development, such as game engines, physics engines, etc.
  13. Scientific computing: C++ language is widely used in scientific computing, numerical computing and other fields, such as numerical simulation, image processing, etc.
  14. Web development: C++ language is also used in Web development, such as using the Cherry framework to develop Web applications.

Although the three languages ​​have some similarities in grammar and structure, their application scenarios and characteristics are different. When choosing a programming language, you need to choose the most suitable language according to the specific application scenarios and needs.

Specifically, HarmonyOS provides the following development tools and components:

  1. ArkUI: A development tool for building user interfaces that supports responsive and adaptive layouts.

ArkUI is a declarative UI development framework for building HarmonyOS application interfaces. It uses minimalist UI information syntax, rich UI components, and real-time preview to improve development efficiency.

  1. ArkJS: A JavaScript runtime environment that supports Node.js and WebAssembly.

ArkJS is a JavaScript application development framework, which is implemented in a variety of programming languages, including JavaScript, C/C++, etc. ArkJS involves multiple components, such as compiler JS front-end, JS back-end, JS runtime, JS debugger, etc. Among them, Ark JS Runtime (Ark JS Runtime) is the runtime used by JavaScript applications on OpenHarmony, including JS object allocator and garbage collector, standard library conforming to ECMAScript specification, interpreter for running Ark bytecode (Ark Bytecode) generated by Ark front-end components, inline cache for storing hidden classes, external function interface (AFFI) and other modules.

  1. ArkCompiler: A compiler that supports AOT and JIT compilation, which can improve the performance and response speed of the application.

ArkCompiler is a brand-new system and application compilation and operation mechanism. It implements static compilation for all Java semantics, directly "translates" the Java language into machine language (static compilation), eliminates the extra overhead of virtual machine dynamic compilation, and realizes the compatibility of development and operation efficiency.

  1. ArkUIWidget: Provides some commonly used UI components, such as buttons, text boxes, etc., to facilitate developers to quickly build user interfaces.

ArkUIWidget is a UI component library launched by Huawei for UI development of HarmonyOS applications. It provides a wealth of UI components and styles, including buttons, forms, pop-up windows, menus, layouts, etc. Developers can directly use these components to quickly build beautiful and easy-to-use UI interfaces. At the same time, ArkUIWidget also supports functions such as custom themes and style extensions, so that developers can customize their own UI styles according to their needs.

  1. ArkModule: A development tool for building modular applications, supporting modular development and modular loading.

ArkModule is a modular application development framework launched by Huawei for the development and management of HarmonyOS applications. It provides a series of modular components and APIs, including application management, module management, service management, data management and other functions. Developers can choose different components and APIs according to their needs to quickly build highly modular and scalable applications. At the same time, ArkModule also provides a series of tools and platform support, such as application packaging, application debugging, application publishing, etc., to facilitate developers to develop, test and release applications.

  1. Ark storage: provides some data storage options, such as local storage, remote storage and database storage.
  2. Ark service: Provides some common services, such as push service, login service, payment service, etc.

In the ArkTS language, to implement the message event refresh card content in HarmonyOS/OpenHarmony application development, the following steps are required:

  1. Define a message event in the card, for example:

@Component({  
  selector: 'my-card',  
  template: `  
    <div>  
      <h1>{
   
   {title}}</h1>  
      <p>{
   
   {content}}</p>  
      <button (click)="onRefresh()">Refresh</button>  
    </div>  
  `,  
  directives: [FORM_DIRECTIVES],  
  events: ['onMessage']  
})  
export class MyCardComponent implements OnInit {  
  title: string;  
  content: string;  
  onMessage(event: any) {  
    // 处理message事件,刷新卡片内容  
    this.title = event.title;  
    this.content = event.content;  
  }  
  ngOnInit() {  
    // 监听message事件,刷新卡片内容  
    this.onMessage({title: 'Title', content: 'Content'});  
  }  
  onRefresh() {  
    // 触发message事件,刷新卡片内容  
    this.onMessage({title: 'New Title', content: 'New Content'});  
  }  
}

This is a code example of the MyCardComponent component, which is used to display the content of a card, including title and content, and provides a refresh button. The component uses the @Component decorator to specify the component's name, template, instructions, and events.

This component uses two-way data binding to display the values ​​of title and content in the template through { {}} syntax. At the same time, it also defines an onMessage method, which is used to handle the message event and refresh the card content when the event occurs. In the ngOnInit method, the component listens to the message event and refreshes the content when the card is initialized. In the onRefresh method, the component triggers the message event and refreshes the card content.

It should be noted that this component uses the FORM_DIRECTIVES directive, so FORM_DIRECTIVES needs to be imported in the component. At the same time, this component also defines an onMessage event, which is used to notify other components or pages to refresh the card content.

 2. Trigger the message event in the parent component, for example:

@Component({  
  selector: 'my-app',  
  template: `  
    <my-card (onMessage)="onMessage($event)"></my-card>  
  `,  
  directives: [MyCardComponent]  
})  
export class AppComponent {  
  onMessage(event: any) {  
    // 触发message事件,刷新卡片内容  
    this.messageEvent = event;  
  }  
}

This is a code example of an AppComponent component that contains the MyCardComponent component and listens to its onMessage event.

The component uses the @Component decorator to specify the name, template, directive and other information of the component. Among them, the MyCardComponent component is used in the template, and (onMessage)="onMessage($event)"its onMessage event is listened to through syntax.

In the onMessage method of AppComponent, it receives the onMessage event passed by MyCardComponent and assigns it to the messageEvent variable. When the messageEvent variable changes, related business logic can be triggered to refresh the card content.

 3. Define the message event in the parent component, for example:

@Component({  
  selector: 'my-app',  
  template: `...`,  
  directives: [MyCardComponent]  
})  
export class AppComponent {  
  messageEvent: any;  
}

This is a code example of an AppComponent component that contains the MyCardComponent component and listens to its onMessage event.

The component uses the @Component decorator to specify the name, template, directive and other information of the component. Among them, the MyCardComponent component is used in the template, and (onMessage)="onMessage($event)"its onMessage event is listened to through syntax.

In AppComponent, it declares a messageEvent variable to store the onMessage event passed by MyCardComponent. When the messageEvent variable changes, related business logic can be triggered to refresh the card content.

In HarmonyOS/OpenHarmony application development, the message event is usually used to transfer data between components or refresh card content. To refresh the card content using the message event, you can follow these steps:

  1. In the MyCardComponent component, define an onMessage method to handle the message event. For example:

    @Component({  
      selector: 'my-card',  
      template: `...`,  
      directives: [FORM_DIRECTIVES],  
      events: ['onMessage']  
    })  
    export class MyCardComponent implements OnInit {  
      title: string;  
      content: string;  
      
      onMessage(event: any) {  
        // 处理message事件,刷新卡片内容  
        this.title = event.title;  
        this.content = event.content;  
      }  
    }

    This is a code example of the MyCardComponent component, which is used to display the content of a card, and listen to the message event to refresh the card content.

    The component uses the @Component decorator to specify the name, template, directive and other information of the component. Among them, the template shows the content of the card, including title and content. At the same time, it uses the FORM_DIRECTIVES directive, so it needs to import FORM_DIRECTIVES in the component.

    The component also defines an onMessage method for handling message events. When the component receives the message event, it will assign the title and content properties in the event to the title and content properties of the component, thereby refreshing the content of the card.

    When using MyCardComponent in the parent component, you can (onMessage)="onMessage($event)"listen to its onMessage event through syntax, and process the event in the onMessage method to refresh the card content.

  2. In the template of the MyCardComponent component, add a button element and bind a click event to trigger the message event. For example:

    <button (click)="onRefresh()">Refresh</button>

    This code is Angular's template syntax, representing a button that triggers the onRefresh method when clicked. Specifically: <button> it is a button element in HTML; (click) it means to listen to the click event;

  3. In the component class, you need to define the onRefresh method: onRefresh() it is a method in the Angular component class to handle the click event.

    import { Component } from '@angular/core';  
      
    @Component({  
      selector: 'app-my-component',  
      template: '<button (click)="onRefresh()">Refresh</button>',  
    })  
    export class MyComponent {  
      onRefresh() {  
        // 点击按钮后执行的逻辑  
      }  
    }
  4. In the onRefresh method, you can write the logic that needs to be executed after clicking the button.

    This is a code example of a MyComponent component, which is used to display a button that triggers the onRefresh method when clicked. Specifically:

  5. import { Component } from '@angular/core': Import Component decorator from Angular's core module;
  6. @Component: Use the Component decorator to define components, including component names, templates, instructions, and other information;
  7. selector: The CSS selector of the specified component, which is used to reference the component in the template;
  8. template: The template of the specified component, where a button is displayed and the click event is bound;
  9. onRefresh(): Defines a method for handling the click event, that is, the logic executed after the button is clicked.
    <app-my-component></app-my-component>

    Here is an example using the MyComponent component. When the component is rendered, it displays a button and listens for click events. When the user clicks the button, the onRefresh method is triggered to execute the corresponding logic.

    In this example, the MyComponent component is <app-my-component></app-my-component>used via syntax. This syntax tells Angular to insert a MyComponent component into the template and render it. When the component is rendered, it @Componentdisplays a button according to the template defined in the decorator and listens for click events. When the user clicks the button, the onRefresh method is triggered to execute the corresponding logic.

  10. In the onRefresh method of the MyCardComponent component, trigger the message event. For example:

    @Component({  
      selector: 'my-app',  
      template: `  
        <my-card (onMessage)="onMessage($event)"></my-card>  
      `,  
      directives: [MyCardComponent]  
    })  
    export class AppComponent {  
      onMessage(event: any) {  
        // 触发message事件,刷新卡片内容  
        this.messageEvent = event;  
      }  
    }

    This is a code example of an AppComponent component that contains the MyCardComponent component and listens to its onMessage event.

    The component uses the @Component decorator to specify the name, template, directive and other information of the component. Among them, the MyCardComponent component is used in the template, and (onMessage)="onMessage($event)"its onMessage event is listened to through syntax.

    In the onMessage method of AppComponent, it receives the onMessage event passed by MyCardComponent and assigns it to its own messageEvent variable. When the messageEvent variable changes, related business logic can be triggered to refresh the card content.

  11. When the user clicks the Refresh button in the MyCardComponent component, MyCardComponent will trigger the message event and pass the new title and content to AppComponent. After AppComponent receives the message event, it will update its own messageEvent variable to refresh the card content.

    When the user clicks the Refresh button in the MyCardComponent component, MyCardComponent will trigger the onRefresh method. In this method, the message event is triggered by calling the raise method, passing the new title and content as parameters to the event.

    (onMessage)="onMessage($event)"The AppComponent component listens to the onMessage event of MyCardComponent through syntax. When AppComponent receives the message event, it will call its own onMessage method. In this method, assign the title and content in the event parameters to its own messageEvent variable.

    Since the messageEvent variable is a property of AppComponent, when it changes, it can trigger related business logic and refresh the card content.

In short, HarmonyOS/OpenHarmony application development requires developers to be familiar with its development framework and tools, and be able to flexibly use multiple languages ​​for development. At the same time, developers also need to pay attention to user experience and performance optimization to ensure the quality and stability of the application.

Guess you like

Origin blog.csdn.net/m0_68231587/article/details/131197227