本小节介绍Stage模型的两种应用组件如何启动FA模型的PageAbility组件。
UIAbility启动PageAbility
UIAbility启动PageAbility和UIAbility启动UIAbility的方式完全相同。
说明: 需注意FA模型中abilityName由bundleName + AbilityName组成,具体见示例。
import common from '@ohos.app.ability.common';
import hilog from '@ohos.hilog';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';const TAG: string = '[EntryAbility]';
const DOMAIN_NUMBER: number = 0xFF00;@Entry
@Component
struct Index {private context = getContext(this) as common.UIAbilityContext;build() {Button('StartFAModel').onClick(() => {let want: Want = {bundleName: 'com.samples.famodelabilitydevelop',abilityName: 'com.samples.famodelabilitydevelop.MainAbility'};this.context.startAbility(want).then(() => {hilog.info(DOMAIN_NUMBER, TAG, 'Start Ability successfully.');}).catch((error: BusinessError) => {hilog.error(DOMAIN_NUMBER, TAG, `Ability failed: ` + JSON.stringify(error));});})}// ...
}
UIAbility访问PageAbility(startAbilityForResult)
startAbilityForResult和startAbility的区别是当PageAbility销毁的时候会返回执行结果。
UIAbility通过startAbilityForResult启动PageABility和UIAbility通过startAbilityForResult启动UIAbility的代码一样,没有任何区别。
import common from '@ohos.app.ability.common';
import hilog from '@ohos.hilog';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';const TAG: string = '[EntryAbility]';
const DOMAIN_NUMBER: number = 0xFF00;@Entry
@Component
struct Index {private context = getContext(this) as common.UIAbilityContext;build() {Button('StartFAModelWithResult').onClick(() => {let want: Want = {bundleName: 'com.samples.stagemodelabilitydevelop',abilityName: 'LifecycleAbility',};// context为调用方UIAbility的UIAbilityContextthis.context.startAbility(want).then(() => {hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in starting LifecycleAbility.');}).catch((err: BusinessError) => {hilog.error(DOMAIN_NUMBER, TAG, `Failed to start LifecycleAbility. Code is ${err.code}, message is ${err.message}`);});})}// ...
}
ExtensionAbility启动PageAbility
下面以ServiceExtensionAbility为例来说明ExtensionAbility启动PageAbility。ServiceExtensionAbility启动PageAbility和ServiceExtensionAbility启动UIAbility的方式完全相同。
import Extension from '@ohos.app.ability.ServiceExtensionAbility'
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';export default class ServiceExtension extends Extension {onCreate(want: Want) {console.info("ServiceExtension onCreate")}onDestroy() {console.info("ServiceExtension onDestroy")}onRequest(want: Want, startId: number) {console.info("ServiceExtension onRequest")let wantFA: Want = {bundleName: "com.ohos.fa",abilityName: "EntryAbility",};this.context.startAbility(wantFA).then(() => {console.info('Start Ability successfully.');}).catch((error: BusinessError) => {console.error("Ability failed: " + JSON.stringify(error));});}
}
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
《鸿蒙开发学习手册》:
如何快速入门:https://qr21.cn/FV7h05
- 基本概念
- 构建第一个ArkTS应用
- ……
开发基础知识:https://qr21.cn/FV7h05
- 应用基础知识
- 配置文件
- 应用数据管理
- 应用安全管理
- 应用隐私保护
- 三方应用调用管控机制
- 资源分类与访问
- 学习ArkTS语言
- ……
基于ArkTS 开发:https://qr21.cn/FV7h05
- Ability开发
- UI开发
- 公共事件与通知
- 窗口管理
- 媒体
- 安全
- 网络与链接
- 电话服务
- 数据管理
- 后台任务(Background Task)管理
- 设备管理
- 设备使用信息统计
- DFX
- 国际化开发
- 折叠屏系列
- ……
鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH
鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向