HarmonyOS5-新闻应用-KnockShare

1作者: zhousg6 天前原帖
HarmonyOS 5 新闻应用 - 敲击分享功能实现案例 摘要 本文详细介绍了在 HarmonyOS 5.0 新闻应用中实现敲击分享功能的过程。使用 KnockManager 类来管理敲击分享事件,从而实现新闻内容的分享。 ```typescript export class KnockManager { private static instance: KnockManager private ctx?: common.UIAbilityContext private isBind: boolean = false private news?: NewsModel static getInstance(ctx: common.UIAbilityContext, news: NewsModel) { if (!KnockManager.instance) { KnockManager.instance = new KnockManager(ctx, news) } return KnockManager.instance } constructor(ctx: common.UIAbilityContext, news: NewsModel) { this.ctx = ctx } // 处理敲击逻辑 knockCallback(target: harmonyShare.SharableTarget) { if (this.news && this.ctx) { // 示例封面是媒体资源,写入沙盒 const media = this.ctx.resourceManager.getMediaContentSync(this.news.cover as Resource) const filePath = this.ctx.filesDir + '/share_' + Date.now() + '.png' const file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE) fileIo.writeSync(file.fd, media.buffer) const uri = fileUri.getUriFromPath(filePath) // 创建分享数据 const shareData: systemShare.SharedData = new systemShare.SharedData({ utd: uniformTypeDescriptor.UniformDataType.HYPERLINK, content: 'https://edition.cnn.com/2025/06/20/sport/lionel-messi-club-world-cup-inter-miami-spt', thumbnailUri: uri, title: this.news.title, description: this.news.company, }) // 敲击分享 target.share(shareData) } } bindEvent() { if (!this.isBind) { harmonyShare.on('knockShare', (target) => { this.knockCallback(target) }) this.isBind = true } } unBindEvent() { harmonyShare.off('knockShare') this.isBind = false } } ```
查看原文
HarmonyOS 5 News Application - Knock Sharing Function Implementation Case Summary This article details the implementation of the knock sharing function in a HarmonyOS 5.0 news application. The KnockManager class is used to manage knock sharing events and achieve the sharing of news content.<p>export class KnockManager { private static instance: KnockManager private ctx?: common.UIAbilityContext private isBind: boolean = false private news?: NewsModel<p><pre><code> static getInstance(ctx: common.UIAbilityContext, news: NewsModel) { if (!KnockManager.instance) { KnockManager.instance = new KnockManager(ctx, news) } return KnockManager.instance } constructor(ctx: common.UIAbilityContext, news: NewsModel) { this.ctx = ctx } &#x2F;&#x2F; handle knock logic knockCallback(target: harmonyShare.SharableTarget) { if (this.news &amp;&amp; this.ctx) { &#x2F;&#x2F; demo cover is media resoure, write in sandbox const media = this.ctx.resourceManager.getMediaContentSync(this.news.cover as Resource) const filePath = this.ctx.filesDir + &#x27;&#x2F;share_&#x27; + Date.now() + &#x27;.png&#x27; const file = fileIo.openSync(filePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE) fileIo.writeSync(file.fd, media.buffer) const uri = fileUri.getUriFromPath(filePath) &#x2F;&#x2F; create share data const sharaData: systemShare.SharedData = new systemShare.SharedData({ utd: uniformTypeDescriptor.UniformDataType.HYPERLINK, content: &#x27; `https:&#x2F;&#x2F;edition.cnn.com&#x2F;2025&#x2F;06&#x2F;20&#x2F;sport&#x2F;lionel-messi-club-world-cup-inter-miami-spt` &#x27;, thumbnailUri: uri, title: this.news.title, description: this.news.company, }) &#x2F;&#x2F; knock share target.share(sharaData) } } bindEvent() { if (!this.isBind) { harmonyShare.on(&#x27;knockShare&#x27;, (target) =&gt; { this.knockCallback(target) }) this.isBind = true } } unBindEvent() { harmonyShare.off(&#x27;knockShare&#x27;) this.isBind = false } }</code></pre>