鸿蒙服务卡片

卡片特点:

  1. 卡片可以承载少量的内容显示和交互

  2. 卡片可以充当元服务的入口,点击卡片可以唤起元服务

服务卡片-应用双向通信

卡片 => 应用

使用postCardAction方法

TypeScript 复制代码
@Entry
@Component
struct MainPic {
  @State
  num: number = 0
  build() {
    Column({ space: 10 }) {
      Row({ space: 10 }) {
        Button("-")
          .onClick(() => {
            this.num && this.num--
          })
        Text(this.num.toString())
          .fontSize(20)
        Button("+")
          .onClick(() => {
            this.num++
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.Center)
    }
    .width('100%')
    .height("100%")
    .justifyContent(FlexAlign.Center)

  }
}

点击卡片唤起应用

TypeScript 复制代码
.onClick(() => {
      postCardAction(this, {
        action: 'router',
        abilityName: 'EntryAbility',
      })
    })

加1减1传递当前的数字

TypeScript 复制代码
@Entry
@Component
struct Count {
  @State
  num: number = 0

  build() {
    Row({ space: 10 }) {
      Button("-")
        .onClick(() => {
          if(this.num)
              this.num--
          postCardAction(this, {
            action: 'call',
            abilityName: 'EntryAbility', // 只能跳转到当前应用下的UIAbility
            params: {
              // 使用call方式 需要第二个参数
              method: 'updateNum', // 名字必须叫method method的值是在ability中监听的方法名
              num: this.num
            }
          })
        })
      Text(this.num.toString())
        .fontSize(20)
      Button("+")
        .onClick(() => {
          this.num++
          postCardAction(this, {
            action: 'call',
            abilityName: 'EntryAbility', // 只能跳转到当前应用下的UIAbility
            params: {
              method: 'updateNum', // 名字必须叫method method的值是在ability中监听的方法名
              num: this.num
            }
          })
        })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
    .alignItems(VerticalAlign.Center)
    .onClick(() => {
      postCardAction(this, {
        action: 'router',
        abilityName: 'EntryAbility'
      })
    })
  }
}

请注意,如果使用call方式传递调用,需要开启一个后台权限-保持应用在后台

TypeScript 复制代码
 "requestPermissions": [{
      "name": "ohos.permission.KEEP_BACKGROUND_RUNNING"
    }],

在ability的onCreate采用callee接收调用方法

callee方法的第一个参数为调用方法名,第二个参数必须返回一个实现rpc.Parcelable的实现类对象

定义一个Params的参数对象

TypeScript 复制代码
import rpc from '@ohos.rpc';
class Params implements rpc.Parcelable {
  marshalling(messageSequence: rpc.MessageSequence): boolean {
    return true;
  }
  unmarshalling(messageSequence: rpc.MessageSequence): boolean {
    return true;
  }
}

在ability中监听callee调用的方法

TypeScript 复制代码
// 声明一个接收参数的对象
class CardParams {
  num: number = 0
}
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.callee.on("updateNum", (data) => {
      const res = JSON.parse(data.readString()) as CardParams
      AppStorage.setOrCreate("num", res.num)
      return new Params()
    })
  }
相关推荐
懂点Tech1 天前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
云和数据.ChenGuang1 天前
Python 重写(Override)深度解析:从 MRO 到 super() 的完整机制
java·开发语言·人工智能·python·langchain·1024程序员节
@大迁世界1 天前
Firefox 本地模式:localhost 再见了 ?
前端·架构
美狐美颜SDK开放平台1 天前
视频美颜SDK试用DEMO阶段应该测试什么?直播APP接入实操指南
开发语言·人工智能·深度学习·音视频·美颜sdk·直播美颜sdk·第三方美颜sdk
dtq04241 天前
C++小案例 - 日志实战
开发语言·c++
星夜离尘561 天前
MATLAB ADC 码流触顶诊断:命中数、缺口分段与削顶不可辨识性
开发语言·matlab
码匠许师傅1 天前
【C++三方组件】Qt 概览:桌面 C++ 的事实标准与许可证三轨
开发语言·c++·qt
码匠许师傅1 天前
【C++三方组件】POCO:跨平台系统编程全家桶
开发语言·c++
2501_948122631 天前
开源鸿蒙平台 CMP 三方库「kmp-showcase-compose」适配全流程
华为·开源·harmonyos
OH_TPC1 天前
HarmonyOS APP开发---“旅图“旅行摄影社区App,需要用到这个库
华为·harmonyos·鸿蒙