HarmonyOS应用开发实战:猫猫大作战-postCardAction 的通信流程
前言postCardAction是服务卡片与 UIAbility 之间的通信机制。当用户在桌面上点击卡片时卡片可以发送消息给所属的 UIAbility实现卡片→应用的跳转和数据传递。本文以「猫猫大作战」的桌面卡片点击进入游戏为锚点讲解 postCardAction 的通信流程。提示本系列不讲 ArkTS 基础语法与环墋搭建。一、卡片点击跳转// ScoreCard.ets — 卡片布局EntryComponentstruct ScoreCard{LocalStorageProp(score)score:string0;build(){Column(){Text( 得分).fontSize(12)Text(this.score).fontSize(24).fontWeight(FontWeight.Bold)}.onClick((){postCardAction(this,{action:router,abilityName:EntryAbility,params:{targetPage:game}});}).width(100%).height(100%).justifyContent(FlexAlign.Center)}}二、在 UIAbility 中接收// EntryAbility.etsexportdefaultclassEntryAbilityextendsUIAbility{onCreate(want:Want){consttargetPagewant.parameters?.targetPageasstring;if(targetPagegame){AppStorage.setOrCreate(targetPage,pages/Index);}}}三、总结postCardAction 实现卡片到应用的点击跳转和数据传递。核心要点postCardAction(component, action)发送卡片事件UIAbility 在onCreate/onNewWant中接收参数下一篇预告第 169 篇将深入 scheduledUpdateTime——卡片定时刷新。如果这篇文章对你有帮助欢迎点赞、收藏⭐、关注你的支持是我持续创作的动力相关资源postCardAction API开源鸿蒙跨平台社区第 167 篇FormExtensionAbility第 169 篇scheduledUpdateTime