Administrator
发布于 2024-06-18 / 49 阅读
0
0

harmony 的卡片 调用 Ability 的函数

//卡片中代码:
@Entry
@Component
struct Widget1Card { //动态卡片  


  @LocalStorageProp('formId') formId: string = '';
  @LocalStorageProp('calleeDetail') calleeDetail: ResourceStr = $r('app.string.app_name');

  isLogin : boolean = true

  build() {
      Column() {

        Text(this.calleeDetail)
          .fontColor('#00f0f1')

        Button('call').onClick(()=>{
          this.calleeDetail = "callFnA "
          postCardAction(this, {
            action: 'call',
            abilityName: 'EntryAbility', // 只能拉起当前应用下的UIAbility
            params: {
              method: 'funA',
              formId: this.formId,
              calleeDetail: 'CallFrom'
            }
          });
        })

      }
      .width('100%')
      .height('100%')

  }


}

EntryFormAbility .onAddForm 获取系统分配的formId

Ablility

import { UIAbility, Want } from '@kit.AbilityKit';
import {  window } from '@kit.ArkUI';
import rpc from '@ohos.rpc';
import formBindingData from '@ohos.app.form.formBindingData';
import formProvider from '@ohos.app.form.formProvider';
import type Base from '@ohos.base';
import { hilog } from '@kit.PerformanceAnalysisKit';


const TAG: string = 'EntryAbility';
const DOMAIN_NUMBER: number = 0xFF00;

export default class EntryAbility extends UIAbility {

  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    
    // 监听call事件所需的方法
    this.callee.on('funA', funACall);

  }

}

const CONST_NUMBER_1: number = 1;

class MyParcelable implements rpc.Parcelable {
  num: number;
  str: string;

  constructor(num: number, str: string) {
    this.num = num;
    this.str = str;
  };

  marshalling(messageSequence: rpc.MessageSequence): boolean {
    messageSequence.writeInt(this.num);
    messageSequence.writeString(this.str);
    return true;
  };

  unmarshalling(messageSequence: rpc.MessageSequence): boolean {
    this.num = messageSequence.readInt();
    this.str = messageSequence.readString();
    return true;
  };
}

// 在收到call事件后会触发callee监听的方法
let funACall = (data: rpc.MessageSequence): MyParcelable => {
  // 获取call事件中传递的所有参数

  hilog.info(0x0000, 'testTag', '%{public}s', 'funACall ');
  let params: Record<string, string> = JSON.parse(data.readString());
  if (params.formId !== undefined) {
    let curFormId: string = params.formId;
    let message: string = params.calleeDetail;
    hilog.info(DOMAIN_NUMBER, TAG, `UpdateForm formId: ${curFormId}, message: ${message}`);
    let formData: Record<string, string> = {
      'calleeDetail': message
    };
    let formMsg: formBindingData.FormBindingData = formBindingData.createFormBindingData(formData);
    formProvider.updateForm(curFormId, formMsg).then((data) => {
      hilog.info(DOMAIN_NUMBER, TAG, `updateForm success. ${JSON.stringify(data)}`);
    }).catch((error: Base.BusinessError) => {
      hilog.error(DOMAIN_NUMBER, TAG, `updateForm failed: ${JSON.stringify(error)}`);
    });
  }
  return new MyParcelable(CONST_NUMBER_1, 'aaa');
};


评论