电子说
本小节介绍Stage模型的两种应用组件如何绑定FA模型ServiceAbility组件。
开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
UIAbility关联访问ServiceAbility和UIAbility关联访问ServiceExtensionAbility的方式完全相同。
import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
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.ServiceAbility',
};
let options: common.ConnectOptions = {
onConnect: (elementName, proxy) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'onConnect called.');
promptAction.showToast({
message: $r('app.string.ConnectFAServiceAbility')
});
},
onDisconnect: (elementName) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'onDisconnect called.');
},
onFailed: (code) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'onFailed code is: ' + code);
}
};
let connectionId = this.context.connectServiceExtensionAbility(want, options);
hilog.info(DOMAIN_NUMBER, TAG, 'connectionId is ' + JSON.stringify(connectionId));
})
}
// ...
}
// ...
}
// ...
}
}
下面以ServiceExtensionAbility为例来说明ExtensionAbility关联访问ServiceAbility。ServiceExtensionAbility关联访问ServiceAbility和ServiceExtensionAbility关联访问ServiceExtensionAbility的方式完全相同。
HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
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.ServiceAbility',
};
let options: common.ConnectOptions = {
onConnect: (elementName, proxy) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'onConnect called.');
promptAction.showToast({
message: $r('app.string.ConnectFAServiceAbility')
});
},
onDisconnect: (elementName) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'onDisconnect called.');
},
onFailed: (code) = > {
hilog.info(DOMAIN_NUMBER, TAG, 'onFailed code is: ' + code);
}
};
let connectionId = this.context.connectServiceExtensionAbility(want, options);
hilog.info(DOMAIN_NUMBER, TAG, 'connectionId is ' + JSON.stringify(connectionId));
})
}
// ...
}
// ...
}
// ...
}
}
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !