實戰(zhàn):貓貓大作戰(zhàn)-postCardAction 的通信流程)
前言postCardAction是服務卡片與 UIAbility 之間的通信機制。當用戶在桌面上點擊卡片時卡片可以發(fā)送消息給所屬的 UIAbility實現(xiàn)卡片→應用的跳轉和數(shù)據(jù)傳遞。本文以「貓貓大作戰(zhàn)」的桌面卡片點擊進入游戲為錨點講解 postCardAction 的通信流程。提示本系列不講 ArkTS 基礎語法與環(huán)墋搭建。一、卡片點擊跳轉// 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 實現(xiàn)卡片到應用的點擊跳轉和數(shù)據(jù)傳遞。核心要點postCardAction(component, action)發(fā)送卡片事件UIAbility 在onCreate/onNewWant中接收參數(shù)下一篇預告第 169 篇將深入 scheduledUpdateTime——卡片定時刷新。如果這篇文章對你有幫助歡迎點贊、收藏?、關注你的支持是我持續(xù)創(chuàng)作的動力相關資源postCardAction API開源鴻蒙跨平臺社區(qū)第 167 篇FormExtensionAbility第 169 篇scheduledUpdateTime