电子说
本小节介绍Stage模型的两种应用组件如何启动FA模型的PageAbility组件。
UIAbility启动PageAbility和UIAbility启动UIAbility的方式完全相同。
说明: 需注意FA模型中abilityName由bundleName + AbilityName组成,具体见示例。 开发前请熟悉鸿蒙开发指导文档 :[
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));
});
})
}
//...
}
//...
}
//...
}
}
startAbilityForResult和startAbility的区别是当PageAbility销毁的时候会返回执行结果。
UIAbility通过startAbilityForResult启动PageABility和UIAbility通过startAbilityForResult启动UIAbility的代码一样,没有任何区别。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));
});
})
}
//...
}
//...
}
//...
}
}
下面以ServiceExtensionAbility为例来说明ExtensionAbility启动PageAbility。ServiceExtensionAbility启动PageAbility和ServiceExtensionAbility启动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对象,客户端获取后便可以与ServiceExtensionAbility进行通信
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');
};
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !