原子化服务作为鸿蒙生态的核心创新,其开发核心围绕 “轻量化、多入口、场景化” 三大原则展开。以下是 “快递查询原子化服务” 的完整开发指南,延续前文框架并补充关键实现细节:
一、原子化服务核心认知:特性与应用场景(续)
场景化服务:聚焦单一高频功能(如快递查询仅保留 “输入单号→查询物流” 核心流程),摒弃冗余功能;
卡片化交互:支持桌面常驻卡片,用户无需启动服务即可查看物流状态,交互效率提升 50% 以上。
典型应用场景
高频工具类:快递查询、二维码生成、单位换算;
即时服务类:外卖点单、电影订票、酒店预订;
信息查询类:天气查询、股票行情、物流跟踪(本文案例)。
二、开发前准备:环境配置与工程创建
1. 环境要求
DevEco Studio:4.1+(支持原子化服务工程模板与 AI 意图框架);
HarmonyOS SDK:API 10+(需包含 “Atomic Service”“Intent Framework” 模块);
测试设备:HarmonyOS 4.0 + 手机 / 平板(支持原子化服务启动与卡片渲染)。
2. 创建原子化服务工程
打开 DevEco Studio,选择 “Create Project”→“Atomic Service” 模板;
配置项目信息:
Project Name:ExpressQueryService;
Bundle Name:com.example.expressquery;
UI Syntax:ArkTS;
Service Type:Feature Ability(功能型原子化服务);
勾选 “Support AI Intent”(启用 AI 意图框架),点击 “Finish”。
3. 核心工程结构
plaintext
ExpressQueryService/
├─ src/main/ets/
│ ├─ abilities/
│ │ └─ ExpressAbility.ets // 服务入口能力
│ ├─ pages/
│ │ └─ QueryPage.ets // 快递查询页面
│ ├─ cards/
│ │ └─ ExpressCard.ets // 桌面卡片(物流状态展示)
│ └─ intents/
│ └─ ExpressIntent.ets // AI意图处理逻辑
└─ src/main/module.json5 // 原子化服务配置(免安装、入口、权限)
三、实战开发:快递查询原子化服务实现
第一步:配置 module.json5 核心参数
json
{
"app": {
"bundleName": "com.example.expressquery",
"versionName": "1.0.0",
"minAPIVersion": 10
},
"module": {
"name": "entry",
"type": "entry",
"installationFree": true, // 声明为免安装原子化服务
"deliveryWithInstall": false,
"mainElement": "com.example.expressquery.ExpressAbility",
"abilities": [
{
"name": ".ExpressAbility",
"type": "feature",
"visible": true,
"exported": true,
"skills": [
{
"entities": ["entity.system.home", "entity.system.search", "entity.system.servicecenter"],
"actions": ["action.system.home", "action.system.search", "action.system.servicecenter"]
}
],
"intents": [
{
"action": "ohos.intent.action.QUERY_EXPRESS", // 自定义AI意图动作
"parameters": [
{ "name": "expressNo", "type": "string" } // 意图参数:快递单号
]
}
]
}
],
"requestPermissions": [
{
"name": "ohos.permission.INTERNET",
"reason": "需要网络权限查询物流信息"
}
]
}
}
第二步:开发核心查询页面(QueryPage.ets)
实现 “输入单号→调用接口→展示物流” 完整流程:
typescript
运行
import http from '@ohos.net.http';
import promptAction from '@ohos.promptAction';
@Entry
@Component
struct ExpressQueryPage {
@State expressNo: string = "";
@State logisticsList: LogisticsItem[] = [];
@State isLoading: boolean = false;
build() {
Column({ space: 20 }) {
Text("快递查询")
.fontSize(26)
.fontWeight(FontWeight.Bold)
.margin({ top: 30 });
// 快递单号输入框
TextInput({ placeholder: "请输入快递单号" })
.width('80%')
.height(50)
.padding(12)
.backgroundColor("#F5F5F5")
.onChange((value) => this.expressNo = value);
// 查询按钮
Button("查询物流")
.width('80%')
.height(50)
.backgroundColor("#007AFF")
.enabled(this.expressNo.length > 0 && !this.isLoading)
.onClick(() => this.queryLogistics());
// 物流列表
if (this.isLoading) {
Text("查询中...")
} else if (this.logisticsList.length > 0) {
List({ space: 10 }) {
ForEach(this.logisticsList, (item) => {
ListItem() {
Column({ space: 5 }) {
Text(item.time)
.fontSize(14)
.color(Color.Gray);
Text(item.desc)
.fontSize(16);
}
.padding(10)
.backgroundColor("#FFFFFF")
.borderRadius(8);
}
});
}
.width('80%')
}
}
.width('100%')
.height('100%')
.backgroundColor("#F5F7FA")
}
// 物流查询逻辑
async queryLogistics() {
this.isLoading = true;
const client = http.createHttp();
try {
const response = await client.request(`https://api.express.com/query?no=${this.expressNo}`, {
method: http.RequestMethod.GET
});
const result = JSON.parse(response.result.toString());
if (result.success) {
this.logisticsList = result.data.logistics;
} else {
promptAction.showToast({ message: "查询失败,请检查单号" });
}
} catch (error) {
promptAction.showToast({ message: "网络异常,请重试" });
} finally {
this.isLoading = false;
client.destroy();
}
}
}
// 物流数据模型
interface LogisticsItem {
time: string;
desc: string;
}
第三步:开发桌面卡片(ExpressCard.ets)
支持用户添加到桌面,实时展示物流状态(2×1 尺寸卡片):
typescript
运行
@Card
@Component
struct ExpressCard {
@Link logistics: LogisticsItem[]; // 与服务页面数据联动
@State expressNo: string = "";
build() {
Column({ space: 10 }) {
Text("物流跟踪")
.fontSize(18)
.fontWeight(FontWeight.Bold);
Text(`单号:${this.expressNo}`)
.fontSize(14)
.color(Color.Gray);
if (this.logistics.length > 0) {
Text(`最新状态:${this.logistics[0].desc}`)
.fontSize(16);
Text(`更新时间:${this.logistics[0].time}`)
.fontSize(14)
.color(Color.Gray);
} else {
Text("暂无物流数据")
.fontSize(14)
.color(Color.Gray);
}
}
.width('100%')
.height('100%')
.padding(12)
.backgroundColor("#FFFFFF");
}
// 卡片点击事件:跳转至查询页面
onClick() {
postCardAction(this, {
action: "router",
abilityName: "ExpressAbility",
params: { expressNo: this.expressNo }
});
}
}
第四步:集成 AI 意图框架(ExpressIntent.ets)
实现 “语音唤醒查询”(如用户说 “查询快递 123456”,自动触发服务):
typescript
运行
import intent from '@ohos.intent';
import featureAbility from '@ohos.ability.featureAbility';
// 注册AI意图处理器
export function registerExpressIntent() {
intent.registerIntentHandler({
action: "ohos.intent.action.QUERY_EXPRESS",
handle: async (intentData) => {
// 解析AI意图中的快递单号
const expressNo = intentData.parameters.expressNo as string;
if (expressNo) {
// 跳转至查询页面并自动查询
await featureAbility.startAbility({
want: {
abilityName: "ExpressAbility",
parameters: { expressNo: expressNo, autoQuery: true }
}
});
}
}
});
}
// 在服务入口Ability中初始化
export default class ExpressAbility extends featureAbility.FeatureAbility {
onCreate(want, launchParam) {
super.onCreate(want, launchParam);
registerExpressIntent(); // 注册AI意图
}
}
四、调试与部署
1. 本地调试
卡片调试:在 DevEco Studio 中打开 “Card Preview”,选择卡片尺寸(2×1),实时预览效果;
AI 意图调试:使用鸿蒙模拟器的 “语音助手”,输入 “查询快递 123456”,验证是否触发服务;
功能调试:连接真实设备,测试单号输入、查询、物流展示全流程。
2. 发布渠道
华为应用市场:提交 HAP 包至 “原子化服务专区”,审核通过后支持搜索、负一屏推荐;
桌面卡片分发:生成卡片分享链接,用户点击即可添加到桌面;
AI 意图市场:在鸿蒙 AI 意图平台注册服务意图,支持语音、文本唤醒。
五、进阶优化
卡片刷新优化:通过 “定时刷新 + 推送刷新” 结合,物流状态更新时自动同步卡片;
多快递公司适配:集成主流快递公司接口,支持自动识别快递公司;
离线缓存:缓存历史查询记录,无网络时展示本地数据;
跨设备协同:手机查询物流,平板桌面卡片展示,多设备数据同步。
原子化服务的核心价值在于 “降低用户使用门槛” 与 “提升服务触达效率”,结合 AI 意图框架后,更能实现 “服务找人” 的主动化体验。开发者可基于本文方案,快速落地工具类、查询类原子化服务,抢占鸿蒙生态流量红利。
相关推荐
写不完的需求
2760
0
1361
0
1656
0
用心写App的人
1926
0雨季
计算机专业学生/从业者,深耕前端开发、C语言及CANN架构,熟系技术栈与工程实践,注重代码优化与问题拆解,以技术落地为核心,热衷AI应用与交互创新,持续精进创值。
帖子
提问
粉丝
《HarmonyOS 原子化服务开发实战:从卡片设计到 AI 意图调用》
2025-11-24 23:00:19 发布HarmonyOS 应用国际化开发指南:多语言适配与全球发布实战
2025-11-23 15:10:12 发布