HarmonyOS-使用call事件拉起指定UIAbility到后台

本文介绍了如何在卡片应用中使用call事件将UIAbility拉到后台,通过按钮触发postCardAction调用指定方法,并传递数据,以实现类似前台的功能,如音乐应用中的播放/暂停控制。
摘要由CSDN通过智能技术生成

使用call事件拉起指定UIAbility到后台

许多应用希望借助卡片的能力,实现和应用在前台时相同的功能。例如音乐卡片,卡片上提供播放、暂停等按钮,点击不同按钮将触发音乐应用的不同功能,进而提高用户的体验。在卡片中使用postCardAction接口的call能力,能够将卡片提供方应用的指定UIAbility拉到后台。同时,call能力提供了调用应用指定方法、传递数据的功能,使应用在后台运行时可以通过卡片上的按钮执行不同的功能。

通常使用按钮控件来触发call事件,示例代码如下:

  • 在卡片页面中布局两个按钮,点击其中一个按钮时调用postCardAction向指定UIAbility发送call事件,并在事件内定义需要调用的方法和传递的数据。需要注意的是,method参数为必选参数,且类型需要为string类型,用于触发UIAbility中对应的方法。

    @Entry
    @Component
    struct WidgetCard {
      build() {
        Column() {
          Button('功能A')
            .margin('20%')
            .onClick(() => {
              console.info('call EntryAbility funA');
              postCardAction(this, {
                'action': 'call',
                'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility
                'params': {
                  'method': 'funA' // 在EntryAbility中调用的方法名
                }
              });
            })
           Button('功能B')
            .margin('20%')
            .onClick(() => {
              console.info('call EntryAbility funB');
              postCardAction(this, {
                'action': 'call',
                'abilityName': 'EntryAbility', // 只能跳转到当前应用下的UIAbility
                'params': {
                  'method': 'funB', // 在EntryAbility中调用的方法名
                  'num': 1 // 需要传递的其他参数
                }
              });
            })
        }
        .width('100%')
        .height('100%')
      }
    }
    
  • 在UIAbility中接收call事件并获取参数,根据传递的method不同,执行不同的方法。其余数据可以通过readString的方式获取。需要注意的是,UIAbility需要onCreate生命周期中监听所需的方法。

    import UIAbility from '@ohos.app.ability.UIAbility';
     
    function FunACall(data) {
      // 获取call事件中传递的所有参数
      console.log('FunACall param:' + JSON.stringify(data.readString()));
      return null;
    }
     function FunBCall(data) {
      console.log('FunACall param:' + JSON.stringify(data.readString()));
      return null;
    }
     
    export default class CameraAbility extends UIAbility {
      // 如果UIAbility第一次启动,在收到call事件后会触发onCreate生命周期回调
      onCreate(want, launchParam) {
          try {
              // 监听call事件所需的方法
              this.callee.on('funA', FunACall);
              this.callee.on('funB', FunBCall);
          } catch (error) {
              console.log('register failed with error. Cause: ' + JSON.stringify(error));
          }
      }
       
      // 进程退出时,解除监听
      onDestroy() {
          try {
              this.callee.off('funA');
              this.callee.off('funB');
          } catch (error) {
              console.log('register failed with error. Cause: ' + JSON.stringify(error));
          }
      }
    };
    
  • 17
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值