论坛首页 漏洞分析研究区 阅读主题

[原创]【FAQ】HarmonyOS SDK 闭源开放能力 — Form Kit

328 浏览 0 回复
#1 楼主 2026-06-01 21:09:23
1.问题描述:
卡片里的Iamge无法显示网络图片。
解决方案:
通过FormExtensionAbility在元服务卡片上加载网络图片有以下两种方案:

方案一:先下载网络图片到本地,参考元服务卡片上加载本地图片示例代码在EntryFormAbility的onAddForm生命周期回调中实现本地文件的刷新。刷新后,在卡片页面通过backgroundImage属性展示EntryFormAbility传递过来的卡片内容。
方案二:申请ohos.permission.INTERNET权限,创建HTTP请求,下载网络图片数据并保存到应用的临时文件中,在EntryFormAbility的onFormEvent生命周期回调中实现网络文件的刷新。刷新后,在卡片页面通过backgroundImage属性展示EntryFormAbility传递过来的卡片内容。使用这种方案在元服务卡片上加载网络图片示例代码详见元服务卡片上加载网络图片指南。

2.问题描述:
如何在主应用中获取卡片的添加/移除?
解决方案:
一、卡片添加/移除的场景以及相关卡片生命周期
由于目前卡片添加/移除至桌面目前没有直接完成的回调响应,可以通过onAddForm和onRemoveForm结合记录保存到桌面的卡片,onAddForm/onRemoveForm添加至桌面的生命周期触发流程如下:

长按应用图标->服务卡片->打开卡片列表,触发onAddForm (卡片列表中有几张卡片生命周期就会回调onAddForm几次)。例如:打开下面的卡片列表,卡片列表中有3张卡片,可以看到日志中OnAddForm回调了3次。
服务卡片列表取消->触发onRemoveForm(卡片列表中有几张卡片生命周期就会回调触发onRemoveForm几次)。 例如:点击x号取消了服务卡片列表,则会出发OnRemoveForm回调3次。
服务卡片列表选择->某一张卡片添加到桌面(卡片生命周期onAddForm选中的卡片,onRemoveForm其余卡片)。例如:添加一张卡片至桌面,则会OnAddForm选中的的卡片,OnRemove另外两张卡片。
移除某一张卡片(卡片生命周期onRemoveForm移除的卡片)。例如:在桌面移除一张卡片,会OnRemove当前移除的卡片。
综上流程,卡片整个添加至桌面的流程中,中间状态生命周期onAddForm/onRemoveForm会回调多次,但是最终卡片新增还是移除是确定的。

二、实现卡片添加/移除管理以及通知主应用的实现
本方案使用首选项通过在服务卡片中onAddForm/onRemoveForm回调函数中,对formId进行持久化缓存,并在主应用中进行跨进程读取。以下是实现步骤:

实现一个公共的单例首选项类以供服务卡片/主应用共同使用,需要注意的是,这里首选项对于跨进程的场景下,需要使用同一个context上下文对象,后面在调用处均使用了this.context.getApplicationContext()保持统一。


export class PreferencesHelper {
private static instance: PreferencesHelper | null = null
private preferences: preferences.Preferences | null = null
private ctx: Context | null = null
private PREFERENCE_NAME:string = "preferences"


public static getInstance(ctx: Context) {
if (PreferencesHelper.instance === null) {
PreferencesHelper.instance = new PreferencesHelper()
PreferencesHelper.instance.init(ctx)
return PreferencesHelper.instance

private init(ctx: Context) {
this.ctx = ctx

public get(key: string, defaultValue: Array<string>) {
if (!this.preferences) {
return
console.log("get ", defaultValue);
preferences.removePreferencesFromCacheSync(this.ctx, options);
return this.preferences.getSync(key, defaultValue)

public put(key: string, defaultValue: preferences.ValueType) {
if (!this.preferences) {
return
console.log("put ", defaultValue);
this.preferences.putSync(key, defaultValue)
this.preferences.flushSync()

public clear() {
PreferencesHelper.instance = null
this.preferences = null


当长按应用图标拉起应用卡片列表时以及在卡片列表中选择一张服务卡片时,会触发onAddForm,此时将卡片id保存下来:(注意:formlist需要做去重)

// 服务卡片FormExtensionAbility.ets
onAddForm(want: Want) {
console.log('onAddForm')
let ctx = this.context.getApplicationContext()
let formList: Array<string> =
PreferencesHelper.getInstance(ctx).get('formList', []) as Array<string>
if (want.parameters) {
let formId = (want.parameters['ohos.extra.param.key.form_identity']) as string
// 为了保证formId的唯一性,需要对formList去重
!formList.includes(formId) &amp;&amp; formList.push(formId)
PreferencesHelper.getInstance(ctx).put('formList', formList)

...(已截断)

---
来源: 看雪论坛
原文链接: https://bbs.kanxue.com/thread-289536.htm

暂无回复,快来抢沙发吧!

请登录后参与讨论

立即登录 注册账号