HarmonyOS 服务卡片中如何进行网络请求?

服务卡片的内容需要网络请求获取,在onAddForm中,请求网络是异步的,如何等待网络请求结束再return formBindingData.createFormBindingData(obj);呢?

阅读 534
1 个回答

不建议在onAddForm中进行网络请求,因为FormExtensionAbility进程不能常驻后台,即在卡片生命周期回调函数中无法处理长时间的任务,在生命周期调度完成后会继续存在5秒,如5秒内没有新的生命周期回调触发则进程自动退出。

针对可能需要5秒以上才能完成的业务逻辑,建议拉起主应用进行处理,处理完成后使用updateForm()通知卡片进行刷新。

参考链接:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-ui-widget-lifecycle-V5

关于卡片的自动刷新逻辑,有如下方案,请参考:

  1. resources-\>profile-\>form\_config.json中widge添加updateEnabled和updateDuration属性,eg:
{
  "name": "dynamic_widget_image",
  "displayName": "$string:dynamic_widget_image_display_name",
  "description": "$string:dynamic_widget_image_desc",
  "src": "./ets/dynamic_widget_image/pages/Dynamic_widget_imageCard.ets",
  "uiSyntax": "arkts",
  "window": {
    "designWidth": 720,
    "autoDesignWidth": true
  },
  "colorMode": "auto",
  "isDynamic": true,
  "isDefault": true,
  "updateEnabled": true,
  "scheduledUpdateTime": "10:30",
  "updateDuration": 1,
  "defaultDimension": "4*4",
  "formConfigAbility": "ability://aaa",
  "supportDimensions": [
    "4*4"
  ]
},
  1. 自动刷新,会被动触发EntryFormAbility下的onUpdateForm方法,在该方法执行updateForm之后,卡片UI所在的page会通过event或caller发送事件

EntryFormAbility.ets

onUpdateForm(formId: string, wantParams?: Record<string, Object>) {
  let formData = new FormDataClass(formId);
  formData.formTime = systemDateTime.getTime().toString();
  let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
  // 执行这里之后,会触发卡片UI,通知卡片UI中的“formId”发生了变化
  formProvider.updateForm(formId, formInfo)
}
  1. 卡片UIpage:
@LocalStorageProp('hotInfos') hotInfos: Array<HotItemData> = [];
@LocalStorageProp('formId') formId: string = "";
@LocalStorageProp('formTime') @Watch("onFormTimeChange") formTime: string = "";

/*
 * 后台拉起EntryAbility,通过updateCardInfo去刷新卡片内容
 * */
onFormTimeChange() {
  postCardAction(this, {
    action: 'call',
    abilityName: 'EntryAbility',
    params: {
      formId: this.formId,
      method: 'updateCardInfo'
    }
  });
}
  1. 最后EntryAbility会响应事件,并进行网络请求,再通过updateForm方法发送事件,EntryFormAbility下的onUpdateForm方法便会被动触发更新,卡片随之更新数据

EntryAbility.ets

onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  Log.i("onCreate want:" + JSON.stringify(want))
  this.callee.on('updateCardInfo', (data: rpc.MessageSequence) => {
  let res = data.readString();
  let formId: string = JSON.parse(res)['formId']
  let formData = new FormDataClass(formId);

  // 这里进行网络请求拿到数据,再去更新
  formData.hotInfos = [{
    title: "新",
    titleBg: "#FFBD33",
    content: "好好补补课"
  },
    {
      title: "荐",
      titleBg: "#338FFF",
      content: "新加坡旅游局官方微博被中国球迷刷屏"
    },
    {
      title: "荐",
      titleBg: "#338FFF",
      content: "女生高考结束直接一睡不起"
    }]
  let formInfo: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
  formProvider.updateForm(formId, formInfo)
  return new MyParcelable(123);
});
this.onHandleWant(want)
}
logo
HarmonyOS
子站问答
访问
宣传栏