API切換概述
FA模型和Stage模型由于線程模型和進(jìn)程模型的差異,部分接口僅在FA模型下才能使用,針對(duì)這部分接口在SDK的接口中有FAModelOnly的標(biāo)記,用于提醒開發(fā)者這部分接口僅能在FA模型下使用。因此在切換到Stage模型時(shí),需要將應(yīng)用中用到的FAModelOnly接口替換成Stage模型下對(duì)應(yīng)的接口。下面是startAbility的接口切換示例,全量接口列表請(qǐng)查看后續(xù)章節(jié):
startAbility接口由FA模型切換到Stage模型的示例: 開發(fā)前請(qǐng)熟悉鴻蒙開發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
- FA模型示例
import featureAbility from '@ohos.ability.featureAbility'; import Want from '@ohos.app.ability.Want'; import hilog from '@ohos.hilog'; const TAG: string = 'PagePageAbilityFirst'; const domain: number = 0xFF00; @Entry @Component struct PagePageAbilityFirst { build() { Column() { List({ initialIndex: 0 }) { ListItem() { Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { //... } .onClick(() = > { (async (): Promise< void > = > { try { hilog.info(domain, TAG, 'Begin to start ability'); let want: Want = { bundleName: 'com.samples.famodelabilitydevelop', moduleName: 'entry', abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton' }; await featureAbility.startAbility({ want: want }); hilog.info(domain, TAG, `Start ability succeed`); } catch (error) { hilog.error(domain, TAG, 'Start ability failed with ' + error); } })() }) } //... } //... } //... } }
- Stage示例示例
HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿
import hilog from '@ohos.hilog';
import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import { Caller } from '@ohos.app.ability.UIAbility';
const TAG: string = '[Page_UIAbilityComponentsInteractive]';
const DOMAIN_NUMBER: number = 0xFF00;
@Entry
@Component
struct Page_UIAbilityComponentsInteractive {
private context = getContext(this) as common.UIAbilityContext;
caller: Caller | undefined = undefined;
build() {
Column() {
//...
List({ initialIndex: 0 }) {
ListItem() {
Row() {
//...
}
.onClick(() = > {
// context為Ability對(duì)象的成員,在非Ability對(duì)象內(nèi)部調(diào)用需要
// 將Context對(duì)象傳遞過去
let wantInfo: Want = {
deviceId: '', // deviceId為空表示本設(shè)備
bundleName: 'com.samples.stagemodelabilitydevelop',
moduleName: 'entry', // moduleName非必選
abilityName: 'FuncAbilityA',
parameters: { // 自定義信息
info: '來自EntryAbility Page_UIAbilityComponentsInteractive頁面'
},
};
// context為調(diào)用方UIAbility的UIAbilityContext
this.context.startAbility(wantInfo).then(() = > {
hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
}).catch((error: BusinessError) = > {
hilog.error(DOMAIN_NUMBER, TAG, 'startAbility failed.');
});
})
}
//...
}
//...
}
//...
}
}
審核編輯 黃宇
-
API
+關(guān)注
關(guān)注
2文章
1499瀏覽量
61962 -
框架
+關(guān)注
關(guān)注
0文章
403瀏覽量
17475 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2339瀏覽量
42805
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論