鸿蒙服务卡片

卡片特点:

  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()
    })
  }
相关推荐
cccc☜42 分钟前
echarts加载地图
前端·javascript·echarts
小禾家的2 小时前
FastReport 加载Load(Stream) 模板内包含换行符不能展示
xml·开发语言
云堆客栈3 小时前
个人毕业设计--基于HarmonyOS的旅行助手APP的设计与实现(挖坑)
华为·harmonyos
冰羽IOX5 小时前
Nginx部署Umi React前端项目标准配置
前端·nginx·react.js·umi·antd
五行星辰5 小时前
用 Java 轻松读取 Word 文档内容
java·开发语言·word
恰小面包5 小时前
react的antd表格自定义图标
前端·javascript·react.js
资深前端之路5 小时前
react面试题三
前端·vue.js·react.js
mqwguardain5 小时前
Python连接不同数据库的总结
开发语言·数据库·python
IT猿手5 小时前
离散浣熊优化算法(DCOA)求解大规模旅行商问题(Large-Scale Traveling Salesman Problem,LTSP),MATLAB代码
开发语言·深度学习·算法·机器学习·matlab
Mr.kanglong6 小时前
【C++】C++11
开发语言·c++