harmony 鸿蒙Application Model Development

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

Application Model Development

How do I obtain a notification when the device orientation changes?

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

How do I redirect a user to a specified page after they touch a service widget?

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

How do I create a background service in the stage model?

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

Can I create a UIAbility and specify the process to run the UIAbility in the FA and Stage models?

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.

What are the differences between the stage model and the FA model in intra-process object sharing?

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

How do I use the lifecycle functions of AbilityStage?

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

How do I delete the mission snapshot in Recents after terminateself is called in the multiton scenario?

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

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

Why can’t I start a UIAbility instance by using startAbility()?

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.

How do I prevent “this” in a method from changing to “undefined” when the method is called?

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.

What should I do when the error message “must have required property ‘startWindowIcon’” is displayed during the UIAbility startup?

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

Can I obtain the context through globalThis in the stage model?

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

What should I do when an error indicating too large size is reported during HAP deployment?

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.

How is data returned when startAbilityForResult is called?

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

How do I obtain the system timestamp?

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

How do I obtain the cache directory of the current application?

Applicable to: OpenHarmony 3.2 Beta5 (API version 9)

Solution

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

Reference

cacheDir

In which JS file is the service widget lifecycle callback invoked?

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

What should I do when the compilation on DevEco Studio fails while ServiceExtensionAbility and DataShareExtensionAbility APIs are used?

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.

How do I obtain the temp and files paths at the application level?

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

Why the application is not deleted from the background mission list after it calls terminateSelf?

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

How does an application developed in the stage model start an application developed in the FA model?

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) => {
    ...
})

Can atomic services be implemented using JavaScript code only?

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.

Can the lifecycle callback of a released FA widget be triggered when the widget is displayed in the service center so that the user login information can be obtained without opening the FA application?

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.

What should I do when the error message “[c4d4d3492eb8531, 0, 0] ContextDeal::startAbility fetchAbilities failed” is displayed during switching to another application?

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.

How do I implement service login by touching a widget?

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

How do I redirect to the application details page in Settings?

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" }
})

How do I get UIAbilityContext within the @Component component in the stage model?

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%')
  }
}

What should I do when starting a continuous task fails?

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

How do FA widgets exchange data?

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  赞