harmony 鸿蒙Application Model Development

  • 2022-10-28
  • 浏览 (1048)

Application Model Development

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Use the UIAbility.onConfigurationUpdate() callback to subscribe to system environment variable changes (including the language, color mode, and screen orientation).

Reference

Subscribing to System Environment Variable Changes

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Configure a widget event with the redirected-to UIAbility specified, and call loadContent in the onWindowStageCreate() callback of the target UIAbility to redirect to the specified page.

Reference

Developing Widget Events

Applicable to: OpenHarmony 3.2 Beta5

Symptom

ServiceExtensionAbility in the stage model is a system API. Therefore, third-party applications cannot use it to create a background service.

Solution

Create a background task to provide the background service.

Reference

Background Task

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Yes.

  • FA model

    The FA model supports multiple processes. By default, all components of an application run in the same process. This default scenario is suitable for most applications. To run a specific component in an independent process, configure the process tag under ability in the configuration file. Note that this tag is available only for system applications.

  • Stage model

    The stage model supports multiple processes. The process model is defined by the system, and third-party applications cannot be configured with multiple processes. To customize an independent process, you must request special permissions, and then specify the process tag under module in the configuration file. This tag specifies the process in which all the abilities in an HAP run. If this tag is not set, the bundle name is used as the process name by default.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

  • In the stage model, multiple application components share the same ArkTS engine instance. Therefore, they can easily share objects and state with each other. This also reduces the memory usage of complex applications.
  • In the FA model, each application component exclusively uses an ArkTS engine instance. Therefore, you are advised to use the stage model when developing complex applications in distributed scenarios.

Reference

Data Synchronization Between UIAbility and UI

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Add the field “srcEntry”: “./ets/myabilitystage/MyAbilityStage.ts” under module in the module.json5 file.

Reference

AbilityStage Component Container

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

You can set removeMissionAfterTerminate to true in the module.json5 file.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

  • If the UIAbility is started using startAbility, check whether the abilityName field in want uses the bundle name as the prefix. If yes, delete the bundle name.
  • Make sure the UIAbility’s home page file configured by onWindowStageCreate in the MainAbility.ts file is defined in the main_pages.json file. You are advised to use the SDK and OpenHarmony SDK versions released on the same day.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Method 1: Add .bind(this) when calling the method.

Method 2: Use the arrow function.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Configure the startWindowIcon attribute under abilities in the module.json5 file.

Example

{
  "module": {
    // Do something.
    "abilities": [{
      // Do something.
      "startWindowIcon": "$media:space",
      "startWindowBackground": "$color:white",
    }]
  }
}

Reference

module.json5 File

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Do not use globalThis to obtain the context in the stage model.

This is because all the processes of an application share a JS VM instance in the stage model. Multiple abilities can run on these processes and share the same global object. If globalThis is used, the context of different abilities of the same JS VM instance may be returned.

Reference

Data Synchronization Between UIAbility and UI

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Symptom

During HAP deployment, the following error message is displayed:

Failure[INSTALL_FAILED_SIZE_TOO_LARGE] error while deploying hap?

Solution

You can split the HAP into multiple HAPs.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

The target UIAbilities uses AbilityContext.terminateSelfWithResult to terminate itself and pass the result to startAbilityForResult.

Reference

Starting UIAbility in the Same Application and Obtaining the Return Result

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Use getCurrentTime of @ohos.systemDateTime to obtain the system time and time zone.

Example

Use the @ohos.systemDateTime API as follows:

```
try {
  systemDateTime.getCurrentTime(true, (error, time) => {
    if (error) {
      console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`);
      return;
    }
    console.info(`Succeeded in getting currentTime : ${time}`);
  });
} catch(e) {
  console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`);
}
```

Reference

System time and time zone

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Use Context.cacheDir to obtain the cache directory of the application.

Reference

cacheDir

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

When a widget is created, a FormAblity.ts file is generated, which contains the widget lifecycle.

Reference

FormExtensionAbility

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Symptom

After the ServiceExtensionAbility and DataShareExtensionAbility APIs are used, DevEco Studio reports an error indicating that the compilation fails.

Cause

The following types of SDKs are provided:

  • Public SDK: intended for application developers and does not contain system APIs that require system permissions.
  • Full SDK: intended for original equipment manufacturers (OEMs) and contains system APIs that require system permissions.

The SDK downloaded using DevEco Studio is the public SDK.

Solution

Third-party application cannot use ServiceExtensionAbility and DataShareExtensionAbility. To develop a system application, first download the full SDK.

Applicable to: OpenHarmony 3.2 Beta5

Solution

Obtain them from the application context. Specifically, use this.context.getApplicationContext.tempDir i to obtain the temp path, and use this.context.getApplicationContext.filesDir to obtain the files path.

Reference

Obtaining Application File Paths

Applicable to: OpenHarmony 3.2 Beta5

Solution

This is because the removeMissionAfterTerminate field under abilities in the module.json5 file of the UIAbility is set to false (default value). To enable the application snapshot to be automatically deleted when the application is destroyed, set this field to true.

Reference

module.json5 File

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Refer to the code snippet below:

let want = {
    deviceId: "", // An empty deviceId indicates the local device.
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility",
    moduleName: "Module1", // moduleName is optional.
    parameters: { // Custom information.
    },
}
// context is the AbilityContext of the FA model to be started.
context.startAbility(want).then(() => {
    ...
}).catch((err) => {
    ...
})

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Currently, the directory structure of new widgets is css+hml+json. This means that the widgets cannot be implemented by using JavaScript code only. Event triggering and parameter transfer can be processed in JSON files.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

After a widget is added, the onCreate() lifecycle is triggered so that related user information (silent login) can be displayed even when the application is not started. However, users must manually add the widget after the application is installed.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Symptom

The startAbility API reports an error during redirection.

Solution

Use startAbility for implementation as follows:

import featureAbility from '@ohos.ability.featureAbility'
function onStartRemoteAbility() {
console.info('onStartRemoteAbility begin');
let params;
let wantValue = {
    bundleName: 'ohos.samples.etsDemo',
    abilityName: 'ohos.samples.etsDemo.RemoteAbility',
    deviceId: getRemoteDeviceId(),
    parameters: params
};
console.info('onStartRemoteAbility want=' + JSON.stringify(wantValue));
featureAbility.startAbility({
    want: wantValue
}).then((data) => {
console.info('onStartRemoteAbility finished, ' + JSON.stringify(data));
});
console.info('onStartRemoteAbility end');
}

Reference

See Starting a Local PageAbility.

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

To create a service widget in the FA model, perform the following steps:

  1. Implement lifecycle callbacks for the widget.

  2. Configure the widget configuration file.

  3. Persistently store widget data.

  4. Update widget data.

  5. Develop the widget UI page.

  6. Develop a widget event. You can start a UIAbility upon the touch and implement service login in the UIAbility.

Reference

Widget Development in the FA Model

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

Refer to the following code:

this.context.startAbility(
{
  action: "action.settings.app.info",
  parameters: { "settingsParamBundleName": "your app bundlename" }
})

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

You can use UIAbility.Context to obtain the context.

Example

import common from '@ohos.app.ability.common';

@Entry
@Component
struct AbilityContextTest {
  // abilityContext
  @State UIAbilityInfo: string = 'Obtaining abilityInfo'
  UIAbilityContext: common.UIAbilityContext

  aboutToAppear() {
    // Use getContext to obtain the context and convert it to abilityContext.
    this.UIAbilityContext = getContext(this) as common.UIAbilityContext
  }

  build() {
    Row() {
      Column({ space: 20 }) {
        Text(this.UIAbilityInfo)
          .fontSize(20)
          .onClick(() => {
            this.UIAbilityInfo = JSON.stringify(this.UIAbilityContext.abilityInfo)
            console.log(`ContextDemo abilityInfo = ${this.UIAbilityInfo}`)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Applicable to: OpenHarmony 3.2 Release (API version 9)

Symptom

A ServiceAbility is started by calling featureAbility.startAbility(). When the ServiceAbility attempts to start a continuous task, the error message {“code”:201,“message”:“BussinessError 201: Permission denied.”} is reported.

Solution

To start a continuous task in the background, you must configure the permission ohos.permission.KEEP_BACKGROUND_RUNNING in the module.json5 file and declare the background mode for the ability that needs to use the continuous task.

"module": {
    "abilities": [
        {
            "backgroundModes": [
            "dataTransfer",
            "location"
            ], // Background mode
        }
    ],
    "requestPermissions": [
        {
            "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" // Continuous task permission
        }
    ]
}

Reference

ServiceAbility Configuration Items - backgroundModes

Continuous Task Permission

Continuous Task

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

The widget interacts with the widget provider through the postCardAction API, and the provider updates data through the updateForm API.

Reference

Widget Development in the FA Model

harmony 鸿蒙FAQs

harmony 鸿蒙Using NDK in a CMake Project

harmony 鸿蒙Application Access Control Development

harmony 鸿蒙ArkUI Animation/Interaction Event Development (ArkTS)

harmony 鸿蒙ArkTS Syntax Usage

harmony 鸿蒙ArkUI Component Development (ArkTS)

harmony 鸿蒙ArkUI Development (JS)

harmony 鸿蒙ArkUI Layout Development (ArkTS)

harmony 鸿蒙ArkUI Routing/Navigation Development (ArkTS)

harmony 鸿蒙Web Development

0  赞
本文目录