11HCIA
HCIA实验。基于
API19
程序框架服务实验
以多
UIAbility的微信软件为案例,创建小程序的UIAbility。模拟微信拉起小程序的效果,即:启动应用内的
UIAbility;模拟在微信中拉起第三方应用如QQ软件,即:实现应用间跳转。
任务一:启动应用内的 UIAbility;
创建项目wechat,并简单设置UI结构。
自行配置项目资源:颜色、图标、app名称、字符串等。

src/main/ets/pages/Index.ets
import { common, Want } from '@kit.AbilityKit'
@Entry
@Component
struct Index {
// 设置微信名称 传递给 被拉起的 UIAbility
wechatName: string = ""
build() {
Column({ space: 30 }) {
Text("模拟微信界面").fontSize(30)
// 启动应用内的 UIAbility;
Column({ space: 10 }) {
TextInput({
placeholder: "请输入你的微信名",
text: $$this.wechatName
})
.width("80%")
.height(40)
Button('单纯的拉起JJ象棋小程序,携带微信名')
.onClick(() => {
// 动态传入微信名,如果用户没输入。设置默认值。
if (this.wechatName === "") {
this.wechatName = "帝心1号"
}
// TODO:拉起小程序
})
}
.ColumnAreaStyle('#51efd5d5')
}
.width('100%')
.height('100%')
.backgroundColor($r('app.color.theme_color'))
}
}
@Extend(Column)
function ColumnAreaStyle(bgc: string) {
.width("90%")
.height(180)
.borderRadius(20)
.backgroundColor(bgc)
.justifyContent(FlexAlign.SpaceEvenly)
}
创建象棋小程序的 UIAbility。
-
在
entry目录上右键新建Ability。正常取名。例如:JJchessAbility -
项目自动生成入口类文件:
src/main/ets/jjchessability/JJchessAbility.ets -
为该Ability配置启动页:
src/main/ets/pages/JJchess.ets
src/main/ets/jjchessability/JJchessAbility.ets
onWindowStageCreate(windowStage: window.WindowStage): void {
windowStage.loadContent("pages/JJchess", (err) => {
if (err.code) {
return;
}
});
}
- 设置
JJchess.ets页面结构
src/main/ets/pages/JJchess.ets
import { common } from '@kit.AbilityKit'
@Entry
@Component
struct JJchess {
build() {
Column({ space: 30 }) {
Text("模拟JJ象棋小程序").fontSize(30)
Column() {
Button('单纯的停止当前小程序')
.onClick(() => {
// TODO 停止当前 UIAbility
})
}
.ColumnAreaStyle('#51efd5d5')
}
.width('100%')
.height('100%')
.backgroundColor($r('app.color.theme_color'))
}
}
@Extend(Column)
function ColumnAreaStyle(bgc: string) {
.width("90%")
.height(180)
.borderRadius(20)
.backgroundColor(bgc)
.justifyContent(FlexAlign.SpaceEvenly)
}
拉起方启动 UIAbility并传递参数(用户输入的微信名)
src/main/ets/pages/Index.ets
- 封装功能函数
// 拉起 JJchessAbility
startJJchessAbility(wechatName: string) {
// 拉起指定的 JJchessAbility 需要一个哥们 Context 帮我在两个 Ability 之间 共享
let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
// 需要want对象 说清楚目标是谁
let wantInfo: Want = {
deviceId: '', // 本设备
// bundleName:context.applicationInfo.name, //可动态获取
bundleName: 'com.dxin.wechat', //也从AppScope/app.json5中查看
moduleName: '', // 目标 模块目录名称 entry 默认同模块
abilityName: 'JJchessAbility',
parameters: {
// wechatName: wechatName
wechatName
}
}
context.startAbility(wantInfo)
}
- 在按钮点击事件中调用功能函数
Button('单纯的拉起JJ象棋小程序,携带微信名')
.onClick(() => {
// 动态传入微信名,如果用户没输入。设置默认值。
if (this.wechatName === "") {
this.wechatName = "帝心1号"
}
this.startJJchessAbility(this.wechatName)
})
被拉起方停止当前UIAbility
src/main/ets/pages/JJchess.ets
- 定义停止自身
UIAbility的功能函数
// 单纯的停止 JJchessAbility
stopJJchessAbility() {
let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
context.terminateSelf() // 停止当前的自己
}
- 在按钮点击事件中调用功能函数
Button('单纯的停止当前小程序')
.onClick(() => {
// TODO 停止当前 UIAbility
this.stopJJchessAbility()
})
任务二:UIAbility 与 UI 的数据同步;
以上仅仅完成了拉起和停止功能。在拉起时还传递了参数(微信名)。可在被拉起方的入口类中获取参数并同步给UI
- 获取拉起方携带的参数并存储
src/main/ets/jjchessability/JJchessAbility.ets
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
// 如果来访有携带数据 可以在此处接受
let wechatName: string = want?.parameters?.wechatName as string || '帝心2号'
// 使用 AppStorage 和 @StorageProp(页面)进行数据同步
AppStorage.setOrCreate("wechatName",wechatName)
}