文章目录
- 1. 实战概述
- 2. 实现步骤
- 2.1 创建鸿蒙应用项目
- 2.2 修改Index.ets代码
- 2.3 创建LuzhouAbility
- 2.4 创建Luzhou页面
- 2.5 设置模块配置文件
- 3. 测试效果
- 4. 实战总结
1. 实战概述
- 本次鸿蒙应用实战,先创建项目“ImplicitWantStartAbility”,接着修改Index.ets等代码构建页面与隐式Want逻辑,创建Luzhou相关文件及设置配置文件匹配条件,最终测试时点击按钮可隐式匹配,实现从首页跳转至泸州页面的效果。
2. 实现步骤
2.1 创建鸿蒙应用项目
- 创建鸿蒙应用项目 -
ImplicitWantStartAbility

- 单击【Finish】按钮,生成应用基本框架

2.2 修改Index.ets代码
- 首页 -
Index.ets

import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG: string = '[Page_Index]';
const DOMAIN_NUMBER: number = 0xFF00;
@Entry
@Component
struct Index {
@State message: string = 'Index页面';
private context = getContext(this) as common.UIAbilityContext;
build() {
Row() {
Column() {
Text(this.message)
.fontSize(40)
.fontWeight(FontWeight.Bold)
.foregroundColor(Color.Yellow)
.margin('10')
Button('去泸州')
.fontSize(40)
.width(250)
.height(70)
.backgroundColor('#44dd22')
.foregroundColor('#ffffff')
.onClick(() => {
let wantInfo: Want = {
action: 'action.huawei.luzhou',
entities: ['entity.huawei.luzhou']
};
this.context.startAbility(wantInfo).then(() => {
hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
}).catch((error: BusinessError) => {
hilog.error(DOMAIN_NUMBER, TAG, 'startAbility failed.');
});
});
}
.width('100%');
}
.height('100%')
.backgroundColor('#00662F')
}
}
- 代码说明:这段代码基于鸿蒙开发框架编写。
Index
组件构建页面,含文本与按钮。点击“去泸州”按钮时,构造含特定action
与entities
的隐式Want
对象,通过上下文尝试启动对应能力,同时利用日志记录启动成功或失败情况,实现交互并监测启动结果。
2.3 创建LuzhouAbility
- 在
ets
里创建LuzhouAbility.ets

- 修改代码,将
pages/Index
改成pages/Luzhou

2.4 创建Luzhou页面
- 在
pages
里创建Luzhou.ets
文件

@Entry
@Component
struct Luzhou {
@State message: string = '泸州欢迎您';
build() {
Row() {
Column() {
Text(this.message)
.fontSize(40)
.fontWeight(FontWeight.Bold)
.foregroundColor(Color.Yellow)
}
.width('100%');
}
.height('100%')
.backgroundColor('#00008B')
}
}
2.5 设置模块配置文件
- 模块配置文件 -
module.json5

- 在
LuzhouAbility
里添加skills
,设置匹配条件

{
"name": "LuzhouAbility",
"srcEntry": "./ets/luzhouability/LuzhouAbility.ets",
"description": "$string:LuzhouAbility_desc",
"icon": "$media:layered_image",
"label": "$string:LuzhouAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"skills": [
{
"entities": [
"entity.huawei.luzhou"
],
"actions": [
"action.huawei.luzhou"
]
}
]
}
3. 测试效果
- 启动应用,显式首页

- 单击【去泸州】按钮,隐式匹配,跳转到泸州页面

4. 实战总结
- 本次鸿蒙应用实战,通过创建项目、修改代码、创建页面及配置文件等系列操作,利用隐式Want实现页面跳转功能。从构建基本框架到完善各模块,最终达成点击按钮顺利切换页面的效果,展示了鸿蒙开发中功能实现的一套有效流程。