OpenHarmony鸿蒙开发学习:【Ability的启动模式】
1.OpenHarmony南向开发案例:【智能风扇】2.鸿蒙HarmonyOS开发实例:【简单时钟】3.HarmonyOS开发实例:【状态管理】4.鸿蒙OS开发学习:【尺寸适配实现】
5.OpenHarmony鸿蒙开发学习:【Ability的启动模式】
6.鸿蒙HarmonyOS开发实战:【分布式音乐播放】7.HarmonyOS开发实例:【分布式数据管理】8.鸿蒙HarmonyOS开发实例:【分布式关系型数据库】9.OpenHarmony开发技术:【国际化】实例10.OpenHarmony开发案例:【分布式计算器】11.HarmonyOS开发实例:【数字管家app】12.HarmonyOS开发:【NFC配置流程】13.HarmonyOS开发:【数字管家app设备接入FA】14.鸿蒙OS开发实例:【HarmonyHttpClient】网络框架15.鸿蒙OS开发实例:【Native C++】16.HarmonyOS开发实例:【菜单app】17.OpenHarmony开发学习:【源码下载和编译】18.HarmonyOS开发实例:【自定义Emitter】19.鸿蒙OS开发学习:【第三方库调用】20.HarmonyOS开发学习:【DevEco Device Tool 安装配置(问题全解)】21.OpenHarmony开发实例:【鸿蒙.bin文件烧录】22.HarmonyOS开发实例:【app帐号管理】23.OpenHarmony实例应用:【常用组件和容器低代码】24.OpenHarmony南向嵌入式:【XR806开发板指导文档】25.OpenHarmony南向开发案例:【智能门锁】26.HarmonyOS开发实例:【任务延时调度】27.OpenHarmony开发案例:【自定义通知】28.OpenHarmony开发实例:【仿桌面应用】29.HarmonyOS开发实例:【事件的订阅和发布】30.OpenHarmony开发案例:【电影卡片】31.OpenHarmony南向开发案例:【智能体重秤】32.鸿蒙OS开发指导:【应用包签名工具】33.OpenHarmony开发实例:【分布式游戏鉴权应用】34.OpenHarmony开发实例:【新闻客户端】35.鸿蒙OpenHarmony Native API【Native_Bundle】36.HarmonyOS开发实战:【亲子拼图游戏】Stage模型中,实现standard、singleton、specified多种模式场景。
本实例参考[开发指南]gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
。 本实例需要使用[aa工具] 查看应用Ability 模式信息。
效果预览
使用说明
或者直接添加
mau123789是v可以直接拿取鸿蒙文档
1、standard模式:
1)进入首页,点击番茄,会新建一个番茄的Ability,展示番茄的详情;
2)在番茄的详情界面,点击黄瓜,会新建一个黄瓜的Ability,展示黄瓜的详情;
3)使用aa工具查看Ability信息,此时存在以下Ability:1个番茄的Ability、1个黄瓜的Ability、1个首页的Ability;
2、singleton模式:
1)进入首页,点击冰淇凌,会新建一个冰淇凌的Ability,展示冰淇凌的详情;
2)在冰淇凌的详情界面,点击螃蟹,会复用冰淇凌的Ability,页面数据会刷新并展示螃蟹的详情;
3)使用aa工具查看Ability信息,此时存在以下Ability:1个冰淇凌的Ability、1个首页Ability;
3、specified模式:
1)进入首页,点击核桃,会新建一个核桃的Ability,展示核桃的详情;
2)在核桃的详情界面,点击蓝莓,会新建一个蓝莓的Ability,展示蓝莓的详情;
3)在蓝莓的详情界面,点击核桃,会复用已存在的核桃的Ability,实现specified模式下的单实例特性,页面数据会刷新并展示核桃的详情;
4)使用aa工具查看Ability信息,此时存在以下Ability:1个核桃的Ability、1个蓝莓的Ability、1个首页Ability;
具体实现
- 本示例启动standard、singleton、specified三种模式的方法主要封装在Util当中,源码参考:[Util.ts]。
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Logger from './Logger'
const TAG = '[Sample_StartMode]'
let contextCaller, want
interface EventHub {
emit(event: string, ...args: Object[]): void
}
interface AbilityContext {
eventHub: EventHub
}
export function getContextData(): any {
Logger.info(TAG, 'UtilPage getContextData start')
let context = getContext(this) as AbilityContext
let data = {
context: null,
launchWant: null
}
context.eventHub.emit("getAbilityData", data)
contextCaller = data.context // 拿到全局的context,即类似globalThis.mainAbilityContext
want = data.launchWant
Logger.info(TAG, 'UtilPage contextCaller ' + JSON.stringify(contextCaller))
return { 'want': want }
}
export function startMode(wantParameters: any, abilityName: string) {
Logger.info(TAG, `${abilityName} start`)
getContextData()
let want = {
bundleName: 'ohos.samples.startmode',
abilityName: abilityName,
parameters: wantParameters
}
Logger.info(TAG, `${abilityName} contextCaller ${JSON.stringify(contextCaller)}`)
contextCaller.startAbility(want).catch(err => {
Logger.info(TAG, 'err is' + JSON.stringify(err))
})
Logger.info(TAG, `${abilityName} end`)
}
export function totast() {
AlertDialog.show(
{
message: $r('app.string.totast'),
secondaryButton: {
value: 'ok',
action: () => {
Logger.info(TAG, 'Callback when the second button is clicked')
}
}
}
)
}
- 新建Ability:创建三个代表standard、singleton、specified模式的Ability,如工程目录中的SingletonAbility、SpecifiedAbility、StandardAbility,并在module.json文件中将launchType属性修改为对应的启动模式属性。
- 启动指定Ability:通过Util中的startMode函数根据页面所传的abilityName,启动对应的ability并进入详情页面。
- specified多实例功能实现:specified模式则是根据MyAbilityStage中的onAcceptWant函数给用户返回一个ability标识,如果之前启动过标识的ability,不创建新的实例并拉回栈顶,否则创建新的实例并启动。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了