HarmonyOS Next 简单上手元服务开发

打印 上一主题 下一主题

主题 1019|帖子 1019|积分 3057

HarmonyOS Next 简单上手元服务开发

万物互联时代,人均持有装备量不停攀升,装备种类和使用场景更加多样,使得应用开发、应用入口变得更加复杂。在此背景下,应用提
供方和用户急迫必要一种新的服务提供方式,使应用开发更简单、服务(如听音乐、打车等)的获取和使用更便捷。为此,HarmonyOS
除支持传统的必要安装的应用(以下简称传统应用)外,还支持更加方便快捷的免安装的应用,即元服务。
元服务是HarmonyOS提供的一种轻量应用程序形态,具备秒开直达,纯净清新;服务相伴,恰适时宜;即用即走,账号相随;一体两
面,嵌入运行;原生智能,全域搜刮;高效开发,生而可信等特征。
元服务可独立上架、分发、运行,独立实现业务闭环,可大幅提拔信息与服务的获取效率。
元服务和应用的的区别


元服务开发旅程


在AGC平台上新建一个项目

链接
一个项目可以多个应用

AGC新建一个元服务应用


新建一个本地元服务项目



如果乐成在AGC平台上新建过元服务,那么这里会主动显示

修改元服务名称


修改元服务图标

紧张,上架审核很严谨


  • 先本身下载随意一张图片
  • 使用画图工具 图像属性 修改 1024px

  • 使用开发工具中 Image Asset 来制作图片


新增元服务卡片

   Win模仿器 不支持新增元服务的卡片
  新建卡片



元服务开发中收到的限定

每一个包巨细不能高出2M


元服务项目总巨细 一样平常是10M,特别环境可以申请20M

服务卡片最多16张

服务卡片不能随意通过卡片跳转其他其他应用或者元服务

服务卡片不能使用call变乱

AtomicServiceTabs

实现tab切换和标题设置

  1. // Index.ets
  2. import { AtomicServiceTabs, TabBarOptions, TabBarPosition, OnContentWillChangeCallback } from '@kit.ArkUI';
  3. @Entry
  4. @Component
  5. struct Index {
  6.   @State message: string = '首页';
  7.   @State currentIndex: number = 0;
  8.   @Builder
  9.   tabContent1() {
  10.     Column().width('100%').height('100%').alignItems(HorizontalAlign.Center).backgroundColor('#00CB87')
  11.   }
  12.   @Builder
  13.   tabContent2() {
  14.     Column().width('100%').height('100%').backgroundColor('#007DFF')
  15.   }
  16.   @Builder
  17.   tabContent3() {
  18.     Column().width('100%').height('100%').backgroundColor('#FFBF00')
  19.   }
  20.   build() {
  21.     Stack() {
  22.       AtomicServiceTabs({
  23.         tabContents: [
  24.           () => {
  25.             this.tabContent1()
  26.           },
  27.           () => {
  28.             this.tabContent2()
  29.           },
  30.           () => {
  31.             this.tabContent3()
  32.           }
  33.         ],
  34.         tabBarOptionsArray: [
  35.           new TabBarOptions($r('sys.media.ohos_ic_public_phone'), '绿色', Color.Black, Color.Blue),
  36.           new TabBarOptions($r('sys.media.ohos_ic_public_location'), '蓝色', Color.Black, Color.Blue),
  37.           new TabBarOptions($r('sys.media.ohos_ic_public_more'), '黄色', Color.Black, Color.Blue)
  38.         ],
  39.         tabBarPosition: TabBarPosition.BOTTOM,
  40.         barBackgroundColor: $r('sys.color.ohos_id_color_bottom_tab_bg'),
  41.       })
  42.     }.height('100%')
  43.   }
  44. }
复制代码
AtomicServiceNavigation

路由管理

Index

  1. // Index.ets
  2. import {
  3.   AtomicServiceNavigation,
  4.   NavDestinationBuilder,
  5.   AtomicServiceTabs,
  6.   TabBarOptions,
  7.   TabBarPosition
  8. } from '@kit.ArkUI';
  9. import { HomeView } from '../views/HomeView';
  10. export interface IParam {
  11.   id: number
  12. }
  13. @Entry
  14. @Component
  15. struct Index {
  16.   @StorageProp("safeTop")
  17.   safeTop: number = 0
  18.   @StorageProp("safeBottom")
  19.   safeBottom: number = 0
  20.   @State message: string = '主题';
  21.   // 页面跳转
  22.   @Provide("pathStack")
  23.   pathStack: NavPathStack = new NavPathStack();
  24.   @Builder
  25.   navigationContent() {
  26.     Button("内容")
  27.       .onClick(() => {
  28.         const param: IParam = { id: 100 }
  29.         this.pathStack.pushPathByName("HomeView", param)
  30.       })
  31.   }
  32.   @Builder
  33.   // 路由页面映射的  以前 navNavigation 修改配置文件!!!
  34.   pageMap(name: string) {
  35.     if (name === 'HomeView') {
  36.       HomeView()
  37.     }
  38.   }
  39.   build() {
  40.     Row() {
  41.       Column() {
  42.         // navNavigation 类似
  43.         AtomicServiceNavigation({
  44.           // 容器内直接显示的内容
  45.           navigationContent: () => {
  46.             this.navigationContent()
  47.           },
  48.           // 标题!!
  49.           title: this.message,
  50.           //
  51.           titleOptions: {
  52.             backgroundColor: '#fff',
  53.             isBlurEnabled: false
  54.           },
  55.           // 路由页面映射
  56.           navDestinationBuilder: this.pageMap,
  57.           navPathStack: this.pathStack,
  58.           mode: NavigationMode.Stack
  59.         })
  60.       }
  61.       .width('100%')
  62.     }
  63.     .height('100%')
  64.     .padding({
  65.       top: this.safeTop,
  66.       bottom: this.safeBottom
  67.     })
  68.   }
  69. }
复制代码
HomeView

  1. import { IParam } from "../pages/Index"
  2. import { promptAction } from "@kit.ArkUI"
  3. @Component
  4. export struct HomeView {
  5.   @Consume("pathStack")
  6.   pathStack: NavPathStack
  7.   aboutToAppear() {
  8.     const param = this.pathStack.getParamByName("HomeView").pop() as IParam
  9.     promptAction.showToast({ message: `${param.id}` })
  10.   }
  11.   build() {
  12.     NavDestination() {
  13.       Column() {
  14.         Text("homeView")
  15.           .fontSize(50)
  16.       }
  17.       .width("100%")
  18.       .height("100%")
  19.       .justifyContent(FlexAlign.Center)
  20.     }
  21.   }
  22. }
复制代码
AtomicServiceWeb

为开发者提供满足定制化诉求的Web高阶组件,屏蔽原生Web组件中无需关注的接口,并提供JS扩展能力
AtomicServiceWeb后续将不再支持registerJavaScriptProxy、runJavaScript等接口。若必要Web组件加载的网页中调用元服务原生页面
的对象方法,可通过JS SDK提供的接口获取相干数据。若JS SDK接口无法满足需求,还可通过传参的方式,元服务原生页面执行对象方法
后获取效果,将效果传递给Web组件加载的网页中。
在元服务内,仅能使用AtomicServiceWeb组件显示Web页面,且必要设置业务域名。
请参考:AtomicServiceWeb组件参考、设置业务域名
根本使用


  • 新建了组件WebHome 用来显示 AtomicServiceWeb容器

      1. import { AtomicServiceWeb, AtomicServiceWebController } from '@ohos.atomicservice.AtomicServiceWeb'
      2. @Entry
      3. @Component
      4. export struct WebHome {
      5.   @State
      6.   ascontroller: AtomicServiceWebController = new AtomicServiceWebController()
      7.   build() {
      8.     NavDestination() {
      9.       Column() {
      10.         AtomicServiceWeb({ src: $rawfile("index.html"), controller: this.ascontroller })
      11.           .width("100%")
      12.           .height("100%")
      13.       }
      14.       .width("100%")
      15.       .height("100%")
      16.       .justifyContent(FlexAlign.Center)
      17.     }
      18.   }
      19. }
      复制代码

  • 新建h5页面 index.html

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.   <meta charset="UTF-8">
    5.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.   <title>Document</title>
    7.   <style>
    8.     body{
    9.       background-color: red;
    10.     }
    11.   </style>
    12. </head>
    13. <body>
    14.   
    15. </body>
    16. </html>
    复制代码
  • 在Index 添加了添加跳转到 WebHome

  • WebHome 使用 AtomicServiceWeb 容器显示内容

鸿蒙页面传递数据到h5页面

通过 src属性传递

h5页面吸收和处理


h5页面调用元服务方法

html中,如果必要调用元服务API,可集成元服务JS SDK,调用相干API进行访问
安装
  1. npm install @atomicservice/asweb-sdk
复制代码
使用方法

es6

  1. import has from '@atomicservice/asweb-sdk';has.asWeb.getEnv({  callback: (err, res) => {  }});
复制代码
umd

  1. <script src="../dist/asweb-sdk.umd.js"></script><script>  has.asWeb.getEnv({    callback: (err, res) => {    }  });</script>
复制代码
更多方法


元服务发送网络请求


  • 申请权限 在AGC平台上 把请求的域名填写到 白名单中 - 上线必须要做

  • 在手机-设置-体系-应用-元服务豁免管控 打开 - 开发阶段也能发送请求
           模仿器不受限定
总结


元服务上架

待续。。。

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

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

王海鱼

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