
前言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