HarmonyOS NEXT实战:元服务的创建、实现多个案例效果

打印 上一主题 下一主题

主题 1018|帖子 1018|积分 3054

什么是元服务

元服务(原名为原子化服务)是HarmonyOS提供的一种面向未来的服务提供方式,是有独立入口、免安装、可为用户提供一个或多个便捷服务的新型应用步伐形态。
创建元服务



  • File–>New–>Create Project,选择

  • 没登录去登录

  • 如果没有APP ID,点







  • 修改图标,点击查看天生元服务图标


  • 注意,这个图标必须用这个,要不然考核会被拒
    您提交的元服务图标与最近任务列表的元服务图标不一致,不符合华为应用市场《元服务考核指南》第1.17项.


开发案例一 单机元服务 【 今日步数 】,正在考核中



  • 先看效果图

  • 定义需要的参数
  1.   // 模拟今日步数
  2.   @Local stepCount: number = 0;
  3.   @Local dateString: string = this.getDate()
复制代码


  • 把 ‘走’ : ‘跑’ 封装到一个@Builder里
  1.   /** 今日步数 */
  2.   @Builder
  3.   doseStatistics(isMonitor: boolean, num: number, used: string, residue: string, value: number, text: string,
  4.     onClick: () => void) {
  5.     Row() {
  6.       Text(isMonitor ? '走' : '跑')
  7.         .width(50)
  8.         .height(50)// .padding(10)
  9.         .textAlign(TextAlign.Center)
  10.         .backgroundColor(isMonitor ? '#EDF3FD' : "#FCEDEC")
  11.         .borderRadius(8)
  12.       Column() {
  13.         Text() {
  14.           Span(isMonitor ? '今日慢走目标' : '今日跑步目标').fontSize(13).fontColor("#222222")
  15.           Span(isMonitor ? ` ${num}步数` : ` ${num}米`).fontSize(13).fontColor("#666666")
  16.         }
  17.         Text() {
  18.           Span(isMonitor ? '已走' : '已跑').fontSize(10).fontColor("#666666")
  19.           Span(` ${used}`)
  20.             .fontSize(14)
  21.             .fontColor(isMonitor ? "#CADDFA" : "#E65441")
  22.           Span(isMonitor ? '  剩余步数' : '  剩余跑步').fontSize(10).fontColor("#666666")
  23.           Span(` ${residue}`).fontSize(14).fontColor("#222222")
  24.         }.margin({ top: 2 })
  25.       }.margin({ left: 8 }).alignItems(HorizontalAlign.Start).layoutWeight(1)
  26.       Stack() {
  27.         Progress({
  28.           value: value,
  29.           total: 10000,
  30.           type: ProgressType.Ring
  31.         })
  32.           .width(64)
  33.           .color(isMonitor ? "#CADDFA" : "#E65441")
  34.           .style({ strokeWidth: 10 })
  35.           .backgroundColor(isMonitor ? '#EDF3FD' : "#FCEDEC")
  36.         Text(text).fontSize(14).fontColor("#222222")
  37.       }
  38.     }.width('100%').margin({ top: 16 }).onClick(() => {
  39.       onClick?.()
  40.     })
  41.   }
复制代码


  • 获取当前时间,年代日分秒
  1.   private getDate() {
  2.     const now = new Date();
  3.     const year = now.getFullYear();
  4.     const month = now.getMonth() + 1; // 注意:月份是从0开始计数的
  5.     const day = now.getDate();
  6.     const hours = now.getHours();
  7.     const minutes = now.getMinutes();
  8.     const seconds = now.getSeconds();
  9.     return `${year}-${month}-${day}-${hours}:${minutes}:${seconds}`
  10.   }
复制代码
开发案例二 单机元服务 【 今日气候 】,正在考核中



  • 先看效果图

  • 今日气候这一部分接纳List+ForEach实现,很简单
  1. List({ space: 30 }) {
  2.         ForEach([1, 2, 3, 4, 5], (item: number) => {
  3.           ListItem() {
  4.             Flex({
  5.               direction: FlexDirection.Column,
  6.               justifyContent: FlexAlign.SpaceBetween,
  7.               alignItems: ItemAlign.Center
  8.             }) {
  9.               Text((9 + item) + ':00')
  10.                 .fontColor(Color.White)
  11.                 .fontSize(15)
  12.               Image((item == 2 || item == 4) ? $r('app.media.dongyu') : $r('app.media.tianqi'))
  13.                 .width(40)
  14.                 .height(40)
  15.                 .objectFit(ImageFit.Auto)
  16.               Text((12 + item) + '℃')
  17.                 .fontColor(Color.White)
  18.                 .fontSize(15)
  19.             }
  20.           }
  21.         })
复制代码


  • 未来四天这一块和上面一样
  1. List({ space: 20 }) {
  2.         ForEach(['二', '三', '四', '五'], (item: string) => {
  3.           ListItem() {
  4.             Row() {
  5.               Text('星期' + item)
  6.                 .fontColor(Color.White)
  7.                 .fontSize(15)
  8.               Image((item == '二' || item == '四') ? $r('app.media.dongyu') : $r('app.media.tianqi'))
  9.                 .width(40)
  10.                 .height(40)
  11.                 .objectFit(ImageFit.Auto)
  12.               Text((item == '二' || item == '四') ? '20℃' : '12℃')
  13.                 .fontColor(Color.White)
  14.                 .fontSize(15)
  15.               Text((item == '二' || item == '四') ? '11℃' : '5℃')
  16.                 .fontColor(Color.White)
  17.                 .fontSize(15)
  18.             }
  19.             .padding({ left: 15, right: 15 })
  20.             .width('100%')
  21.             .justifyContent(FlexAlign.SpaceBetween)
  22.           }
复制代码
开发案例三 单机元服务 【 公共交通出行 】,正在考核中



  • 先看效果图

  • 用户信息
  1.   @Builder
  2.   userInfo() {
  3.     Row() {
  4.       Image($r('app.media.ic_my_avatar'))
  5.         .width(44)
  6.       Column() {
  7.         Text('张三')
  8.           .fontSize(18)
  9.           .fontColor(Color.White)
  10.         Text('VIP标识')
  11.           .fontSize(10)
  12.           .margin({ top: 5 })
  13.           .fontColor(Color.Yellow)
  14.       }
  15.       .alignItems(HorizontalAlign.Start)
  16.       .layoutWeight(1)
  17.       .margin({ left: 8 })
  18.       Text() {
  19.         Span('会员剩余')
  20.         Span(' 9 ').fontSize(18).fontColor("#FEDB9B")
  21.         Span('天')
  22.       }
  23.       .height(32)
  24.       .fontColor("#FEDB9B")
  25.       .fontSize(10)
  26.       .padding({ left: 16, right: 12 })
  27.       .borderRadius({ topLeft: 20, bottomLeft: 20 })
  28.       //渐变色
  29.       .linearGradient({
  30.         angle: 135,
  31.         colors: [["#1D2432", 0.2], ['#3E4A61', 0.8]]
  32.       })
  33.     }
  34.     .padding({ bottom: 10 })
  35.     .width('100%')
  36.     .margin({ top: 46, left: 12 })
  37.     .onClick(() => {
  38.     })
  39.   }
复制代码


  • 公共交通功能部分代码
  1. Row() {
  2.           this.msgRelated($r('app.media.1'), '新能源导航', () => {
  3.             promptAction.showToast({ message: '新能源导航' })
  4.           })
  5.           this.msgRelated($r('app.media.j2'), '货车', () => {
  6.           })
  7.           this.msgRelated($r('app.media.j3'), '摩托车', () => {
  8.           })
  9.           this.msgRelated($r('app.media.j4'), '骑行', () => {
  10.           })
  11.         }
  12.         .width('95%')
  13.         .height(80)
  14.         .margin({
  15.           top: 10,
  16.           left: 12,
  17.           right: 12
  18.         })
  19.         .backgroundColor(Color.White)
  20.         .justifyContent(FlexAlign.SpaceAround)
复制代码
上一篇文章>>>>>>>>>>>

HarmonyOS NEXT实战:自定义封装多种样式导航栏组件
若本文对您稍有帮助,诚望您不吝点赞,多谢。
有兴趣的同砚可以点击查看源码



  • gitee:https://gitee.com/jiaojiaoone/explore-harmony-next/tree/case%2Fwanandroid/
  • github:https://github.com/JasonYinH/ExploreHarmonyNext.git
接待加我微信一起互换:+V:yinshiyuba


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

道家人

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表