HarmonyOS 鸿蒙Next form Kit服务卡片,EntryFormAbility使用PersistentStorage和AppStorage不生效

发布于 1周前 作者 eggper 最后一次编辑是 5天前 来自 鸿蒙OS

HarmonyOS 鸿蒙Next form Kit服务卡片,EntryFormAbility使用PersistentStorage和AppStorage不生效

Form Kit服务卡片,EntryFormAbility使用PersistentStorage和AppStorage不生效

  1. 想把所有加桌的formId缓存起来(鸿蒙SDK未提供获取目标应用所有formId的方法,只能开发者自己缓存)
  2. 在EntryFormAbility里的onAddForm方法中AppStorage.set缓存,onRemoveForm方法里delete
  3. 在利用PersistentStorage持久化
  4. 在EntryAbility的里AppStorage.get 获取缓存,发现读取不到
  5. 可以用db实现,但是存几个formId用db感觉有点复杂

请问EntryFormAbility是不是不支持与EntryAbility通过 PersistentStorage和AppStorage 共享数据?

import { formBindingData, FormExtensionAbility, formInfo } from ‘[@kit](/user/kit).FormKit’;
import { Want } from ‘[@kit](/user/kit).AbilityKit’;
import { JSON } from ‘[@kit](/user/kit).ArkTS’;
import { hilog } from ‘[@kit](/user/kit).PerformanceAnalysisKit’;

PersistentStorage.persistProp(‘WidgetCardFormIdSet’, new Set<string>([]));


export default class EntryFormAbility extends FormExtensionAbility {
onAddForm(want: Want) {
hilog.info(0x0000, ‘EntryFormAbility’, ‘onAddForm WidgetCardFormIdSet Size: %{public}s’,
AppStorage.get<Set<string>>(‘WidgetCardFormIdSet’)?.size);


let formId = want?.parameters?.[formInfo.FormParam.IDENTITY_KEY] as string
AppStorage.set(‘WidgetCardFormIdSet’, AppStorage.get<Set<string>>(‘WidgetCardFormIdSet’)?.add(formId))
let formData = ‘’;
return formBindingData.createFormBindingData(formData);
}


onCastToNormalForm(formId: string) {
hilog.info(0x0000, ‘EntryFormAbility’, ‘onCastToNormalForm formId: %{public}s’, formId);
// Called when the form provider is notified that a temporary form is successfully
// converted to a normal form.
}


onUpdateForm(formId: string) {
hilog.info(0x0000, ‘EntryFormAbility’, ‘onUpdateForm formId: %{public}s’, formId);
// Called to notify the form provider to update a specified form.
}


onFormEvent(formId: string, message: string) {
hilog.info(0x0000, ‘EntryFormAbility’, ‘onFormEvent formId: %{public}s’, formId);
// Called when a specified message event defined by the form provider is triggered.
}


onRemoveForm(formId: string) {
hilog.info(0x0000, ‘EntryFormAbility’, ‘onRemoveForm formId: %{public}s’, formId);
AppStorage.set(‘WidgetCardFormIdSet’, AppStorage.get<Set<string>>(‘WidgetCardFormIdSet’)?.delete(formId))
// Called to notify the form provider that a specified form has been destroyed.
}


onAcquireFormState(want: Want) {
hilog.info(0x0000, ‘EntryFormAbility’, ‘onAcquireFormState Want: %{public}s’, JSON.stringify(want));
// Called to return a {[@link](/user/link) FormState} object.
return formInfo.FormState.READY;
}
}

更多关于HarmonyOS 鸿蒙Next form Kit服务卡片,EntryFormAbility使用PersistentStorage和AppStorage不生效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS 鸿蒙Next form Kit服务卡片,EntryFormAbility使用PersistentStorage和AppStorage不生效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS 鸿蒙Next form Kit服务卡片中EntryFormAbility使用PersistentStorage和AppStorage不生效的问题,这通常可能是由于多种原因导致的。以下是一些可能的解决方案:

  1. 检查存储初始化:确保在使用PersistentStorage和AppStorage之前,它们已经被正确初始化。对于PersistentStorage,需要调用其初始化方法;对于AppStorage,则通常由UI框架在应用启动时创建。
  2. 存储权限:确认应用是否具有足够的权限来访问和修改存储。在某些情况下,权限问题可能导致存储操作失败。
  3. 存储位置:服务卡片和主应用可能使用不同的上下文(Context),导致它们访问的存储位置不同。确保在EntryFormAbility中使用正确的上下文来访问存储。
  4. 数据同步:对于AppStorage,如果需要在多个页面或组件间共享数据,确保数据同步机制已正确设置。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部