Stage模型啟動(dòng)FA模型PageAbility
本小節(jié)介紹Stage模型的兩種應(yīng)用組件如何啟動(dòng)FA模型的PageAbility組件。
UIAbility啟動(dòng)PageAbility
UIAbility啟動(dòng)PageAbility和UIAbility啟動(dòng)UIAbility的方式完全相同。
說(shuō)明: 需注意FA模型中abilityName由bundleName + AbilityName組成,具體見(jiàn)示例。 開(kāi)發(fā)前請(qǐng)熟悉鴻蒙開(kāi)發(fā)指導(dǎo)文檔 :[
gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG: string = '[Page_StartFAModel]';
const DOMAIN_NUMBER: number = 0xFF00;
@Entry
@Component
struct Page_StartFAModel {
private context = getContext(this) as common.UIAbilityContext;
build() {
Column() {
//...
List({ initialIndex: 0 }) {
ListItem() {
Row() {
//...
}
.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訪問(wèn)PageAbility(startAbilityForResult)
startAbilityForResult和startAbility的區(qū)別是當(dāng)PageAbility銷毀的時(shí)候會(huì)返回執(zhí)行結(jié)果。
UIAbility通過(guò)startAbilityForResult啟動(dòng)PageABility和UIAbility通過(guò)startAbilityForResult啟動(dòng)UIAbility的代碼一樣,沒(méi)有任何區(qū)別。HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿
import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { promptAction } from '@kit.ArkUI';
const TAG: string = '[Page_StartFAModel]';
const DOMAIN_NUMBER: number = 0xFF00;
@Entry
@Component
struct Page_StartFAModel {
private context = getContext(this) as common.UIAbilityContext;
build() {
Column() {
//...
List({ initialIndex: 0 }) {
ListItem() {
Row() {
//...
}
.onClick(() = > {
let want: Want = {
bundleName: 'com.samples.famodelabilitydevelop',
abilityName: 'com.samples.famodelabilitydevelop.MainAbility',
};
this.context.startAbilityForResult(want).then((result) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'Ability verify result: ' + JSON.stringify(result));
if (result !== null) {
promptAction.showToast({
message: JSON.stringify(result)
});
}
}).catch((error: BusinessError) = > {
hilog.error(DOMAIN_NUMBER, TAG, `Ability failed: ` + JSON.stringify(error));
});
})
}
//...
}
//...
}
//...
}
}
ExtensionAbility啟動(dòng)PageAbility
下面以ServiceExtensionAbility為例來(lái)說(shuō)明ExtensionAbility啟動(dòng)PageAbility。ServiceExtensionAbility啟動(dòng)PageAbility和ServiceExtensionAbility啟動(dòng)UIAbility的方式完全相同。
import { Want, ServiceExtensionAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { rpc } from '@kit.IPCKit';
import ServiceExtImpl from '../IdlServiceExt/idl_service_ext_impl';
const TAG: string = '[ServiceExtAbility]';
const DOMAIN_NUMBER: number = 0xFF00;
export default class ServiceExtAbility extends ServiceExtensionAbility {
serviceExtImpl: ServiceExtImpl = new ServiceExtImpl('ExtImpl');
onCreate(want: Want): void {
let serviceExtensionContext = this.context;
hilog.info(DOMAIN_NUMBER, TAG, `onCreate, want: ${want.abilityName}`);
};
onRequest(want: Want, startId: number): void {
hilog.info(DOMAIN_NUMBER, TAG, `onRequest, want: ${want.abilityName}`);
if (want.parameters?.key === 'ConnectFaPageAbility') {
let wantFA: Want = {
bundleName: 'com.samples.famodelabilitydevelop',
abilityName: 'com.samples.famodelabilitydevelop.MainAbility',
};
this.context.startAbility(wantFA).then(() = > {
hilog.info(DOMAIN_NUMBER, TAG, 'Start Ability successfully.');
}).catch((error: BusinessError) = > {
hilog.info(DOMAIN_NUMBER, TAG, `Ability failed: ${JSON.stringify(error)}`);
});
}
};
onConnect(want: Want): rpc.RemoteObject {
hilog.info(DOMAIN_NUMBER, TAG, `onConnect, want: ${want.abilityName}`);
// 返回ServiceExtImpl對(duì)象,客戶端獲取后便可以與ServiceExtensionAbility進(jìn)行通信
return this.serviceExtImpl as rpc.RemoteObject;
};
onDisconnect(want: Want): void {
hilog.info(DOMAIN_NUMBER, TAG, `onDisconnect, want: ${want.abilityName}`);
};
onDestroy(): void {
hilog.info(DOMAIN_NUMBER, TAG, 'onDestroy');
};
}
審核編輯 黃宇
-
框架
+關(guān)注
關(guān)注
0文章
403瀏覽量
17510 -
模型
+關(guān)注
關(guān)注
1文章
3261瀏覽量
48914 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2369瀏覽量
42900
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論