DEV Community

Cover image for HarmonyOS Native Development Notes: 02 - Service Card Development
shaohushuo
shaohushuo

Posted on

HarmonyOS Native Development Notes: 02 - Service Card Development

Introduction

Service cards are desktop widgets that can be placed on the desktop and other locations, and can be reached at a touch.

Service cards are divided into two categories: static cards and dynamic cards. This article introduces static cards.

Create

Back to DevEco, right-click in the directory entry, click Create Service Widget, select Static Widget, and click Next.

Enter a name, select a supported card size, and click OK to create the card.

2*2 represents 2 rows and 2 columns, and 1*2 represents 1 row and 2 columns.

Write the card interface

Interaction

Click event parameter transmission

Here ArkUI is used to write the interface, but click events cannot be used. Instead, FormLink should be used. The related events are received on the formability side. Through different parameters, router.push is called to open different pages.

FormLink({ 
action: this.ACTION_TYPE, 
abilityName: this.ABILITY_NAME, 
params: { 
action: this.MESSAGE 
} 
}) { 
... 
}
Enter fullscreen mode Exit fullscreen mode

Parameter reception

In the onCreate and onNewWant life cycles in entryability, to receive parameters

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 

if(want?.parameters?.params) { 
let params: Record<string, Object> = JSON.parse(want.parameters.params as string); 
this.selectPage = params.action as string; 
console.log("selectPage", this.selectPage); 
} 
} 

onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { console.log('onNewWant');
if (want?.parameters?.params) {
// want.parameters.params corresponds to the content of params in postCardAction()
let params: Record<string, Object> = JSON.parse(want.parameters.params as string);
this.selectPage = params.action as string;
hilog.info(DOMAIN_NUMBER, TAG, `onNewWant selectPage: ${this.selectPage}`);
}
if (this.currentWindowStage !== null) {
this.onWindowStageCreate(this.currentWindowStage);
}
}
Enter fullscreen mode Exit fullscreen mode

Notes

  1. When running, please use normal mode. Service cards do not support HotReload, and cards cannot be displayed normally in hot reload mode.

References

Top comments (0)