HarmonyOS 鸿蒙Next服务卡片内容是否可通过Ability配置 例如:我的App有生日列表,但服务卡片仅展示某一生日信息需用户选择

发布于 1周前 作者 wuwangju 来自 鸿蒙OS

HarmonyOS 鸿蒙Next服务卡片内容是否可通过Ability配置
例如:我的App有生日列表,但服务卡片仅展示某一生日信息需用户选择

您好,服务卡片内容是否可以通过Ability配置? 例如:我的App有生日列表,但是我的服务卡片只展示某一个生日信息,这个需要用户去选择。  安卓的话是可以实现的,配置appwidget-provider标签的android:configure属性就可以。

2 回复
您可以在卡片的生命周期onAddForm中获取到卡片的Id,通过首选项Preferences存储Id,而后在页面中通过formProvider.updateForm来实现在页面中选取不同的卡片信息进行展示,可以参考一下以下代码
//WidgetCard.ets 
let storageUpdateRouter = new LocalStorage();
[@Entry](/user/Entry)(storageUpdateRouter)
[@Component](/user/Component)
struct WidgetUpdateRouterCard {
  [@LocalStorageProp](/user/LocalStorageProp)('routerDetail') routerDetail: string = 'aaaaa';
  build() {
    Column() {
      Column() {
        Text(this.routerDetail)
          .fontColor(Color.Black)
          .opacity(0.9)
          .fontSize(14)
          .margin({ top: '8%', left: '10%', right: '10%' })
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .maxLines(2)
      }.width('100%').height('50%')
      .alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .height('100%')
    .alignItems(HorizontalAlign.Start)
    .backgroundImageSize(ImageSize.Cover)
  }
}
//EntryFormAbility.ets
import { formBindingData, FormExtensionAbility, formInfo } from '@kit.FormKit';
import { Want } from '@kit.AbilityKit';
import { preferences } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryFormAbility extends FormExtensionAbility {
  onAddForm(want: Want) {
    let dataPreferences: preferences.Preferences | null = null;
    if (want.parameters) {
      let formID = JSON.stringify(want.parameters[formInfo.FormParam.IDENTITY_KEY]);
      preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: preferences.Preferences) => {
        if (err) {
          return;
        }
        AppStorage.setOrCreate('cardId', formID)
        dataPreferences = val;
        dataPreferences.put('formID', formID)
        dataPreferences.flush()
      })
    }
    // let formId: string = ;
    // Called to return a FormBindingData object.
    let formData = '';
    return formBindingData.createFormBindingData(formData);
  }
  onCastToNormalForm(formId: string) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }
  onUpdateForm(formId: string) {
    // Called to notify the form provider to update a specified form.
  }
  onFormEvent(formId: string, message: string) {
    // Called when a specified message event defined by the form provider is triggered.
  }
  onRemoveForm(formId: string) {
    // Called to notify the form provider that a specified form has been destroyed.
  }
  onAcquireFormState(want: Want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};
//index.est
import { formBindingData, formInfo, formProvider } from '@kit.FormKit';
import { BusinessError } from '@ohos.base';
import { preferences } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
  [@State](/user/State) message: string = 'Hello World';
  build() {
    Column() {
      Button('卡片类型1').onClick((event: ClickEvent) => {
        let context = getContext(this) as common.UIAbilityContext
        let dataPreferences: preferences.Preferences | null = null;
        preferences.getPreferences(context, 'myStore', (err: BusinessError, val: preferences.Preferences) => {
          if (err) {
            console.error(""Failed to get preferences. code ="" + err.code + "", message ="" + err.message);
            return;
          }
          dataPreferences = val;
          dataPreferences.get('formID', 'string').then((res: preferences.ValueType) => {
            //去除首选项所携带的引号
            let cardId = res.toString().replace(/""/g, '')
            let formData: Record<string, string> = {
              'routerDetail': '卡片类型1', // 和卡片布局中对应
            };
            let formMsg = formBindingData.createFormBindingData(formData);
            formProvider.updateForm(cardId, formMsg)
          })
        })
      })
      Button('卡片类型2').onClick((event: ClickEvent) => {
        let context = getContext(this) as common.UIAbilityContext
        let dataPreferences: preferences.Preferences | null = null;
        preferences.getPreferences(context, 'myStore', (err: BusinessError, val: preferences.Preferences) => {
          if (err) {
            console.error(""Failed to get preferences. code ="" + err.code + "", message ="" + err.message);
            return;
          }
          dataPreferences = val;
          dataPreferences.get('formID', 'string').then((res: preferences.ValueType) => {
            let cardId = res.toString().replace(/""/g, '')
            let formData: Record<string, string> = {
              'routerDetail': '卡片类型2', // 和卡片布局中对应
            };
            let formMsg = formBindingData.createFormBindingData(formData);
            formProvider.updateForm(cardId, formMsg)
          })
        })
      })
      Button('卡片类型3').onClick((event: ClickEvent) => {
        let context = getContext(this) as common.UIAbilityContext
        let dataPreferences: preferences.Preferences | null = null;
        preferences.getPreferences(context, 'myStore', (err: BusinessError, val: preferences.Preferences) => {
          if (err) {
            return;
          }
          dataPreferences = val;
          dataPreferences.get('formID', 'string').then((res: preferences.ValueType) => {
            let cardId = res.toString().replace(/""/g, '')
            let formData: Record<string, string> = {
              'routerDetail': '卡片类型3', // 和卡片布局中对应
            };
            let formMsg = formBindingData.createFormBindingData(formData);
            formProvider.updateForm(cardId, formMsg)
          })
        })
      })
    }
  }
} 

该方法需新建立好卡片

目前鸿蒙没有相关的实现能力,您这边可以在代码中创建多个卡片,用户在创建卡片时,可以选择不同的卡片进行创建,这样应该是可以满足您的需求的

用户在创建卡片的页面处,可以通过左右滑动选择不同的卡片

HarmonyOS 鸿蒙Next服务卡片内容可通过Ability配置。在HarmonyOS中,服务卡片的内容展示与更新通常依赖于卡片提供方(即App)与卡片使用方(如负一屏)之间的交互。针对帖子中提到的场景,即App有生日列表但服务卡片仅展示某一生日信息且需用户选择,这可以通过以下方式实现:

  1. 配置卡片能力:在App的module.json5配置文件中,为服务卡片配置FormExtensionAbility,并指定卡片的具体配置信息文件(如form_config.json)。
  2. 实现卡片生命周期接口:在App中实现FormExtensionAbility的生命周期接口,如onUpdateForm,用于更新卡片内容。
  3. 数据交互:当用户选择生日列表中的某一项时,通过调用updateForm方法并传递相应的数据,来更新服务卡片上的展示内容。

通过这种方式,可以实现HarmonyOS鸿蒙Next服务卡片内容的动态配置与更新。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部