全屏启动元服务组件,当提供方授权使用方嵌入式运行元服务时,使用方全屏嵌入式运行元服务;未授权时,使用方跳出式拉起元服务。
该组件从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
当需要在该组件中实现可嵌入式运行的元服务,必须继承自EmbeddableUIAbility。否则,系统无法保证元服务功能正常。
FullScreenLaunchComponent({ content: Callback<void>, appId: string, options?: AtomicServiceOptions, onError?: ErrorCallback, onTerminated?: Callback<TerminationInfo>, onReceive?: Callback<Record<string, Object>> })
装饰器类型:@Component
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
| 名称 | 类型 | 必填 | 装饰器类型 | 说明 |
|---|---|---|---|---|
| content | Callback<void> | 是 | @BuilderParam | 可以使用组件组合来自定义拉起元服务前的占位图标,实现类似大桌面应用图标的效果。点击占位组件后,将拉起元服务。 |
| appId | string | 是 | - | 需要拉起的元服务appId,appId是元服务的唯一标识。 |
| options | AtomicServiceOptions | 否 | - | 拉起元服务的参数。不填时使用默认参数拉起元服务。 |
| onError18+ | ErrorCallback | 否 | - | 被拉起的嵌入式运行元服务在运行过程中发生异常时触发本回调。可通过回调参数中的code、name和message获取错误信息并做处理。 元服务API: 从API version 18开始,该接口支持在元服务中使用。 |
| onTerminated18+ | Callback<TerminationInfo> | 否 | - | 被拉起的嵌入式运行元服务通过点击元服务退出按钮、手势侧滑、调用terminateSelfWithResult或者terminateSelf正常退出时,触发本回调函数。 元服务API: 从API version 18开始,该接口支持在元服务中使用。 |
| onReceive20+ | Callback<Record<string, Object>> | 否 | - | 被拉起的嵌入式运行元服务通过@ohos.window (窗口)调用相关API时,触发本回调。 元服务API: 从API version 20开始,该接口支持在元服务中使用。 |
本示例展示组件使用方法和提供方元服务的实现。实际运行时请使用开发者自己的元服务appId。
FullScreenLaunchComponent组件需要由使用方调用。在提供方完成本地的安装后,即可在使用方应用或者元服务中全屏嵌入式拉起提供方的元服务。
由于嵌入式元服务运行在独立进程,其崩溃异常不会直接暴露在宿主的日志中。本地调试时可通过以下方式查看真实报错栈:
使用方
// 使用方入口界面Index.ets内容如下:
import { FullScreenLaunchComponent } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State appId: string = '6917573653426122083'; // 元服务appId
build() {
Row() {
Column() {
FullScreenLaunchComponent({
content: ColumnChild,
appId: this.appId,
options: {},
onTerminated: (info) => {
console.info(`onTerminated code: ${info.code.toString()}`);
},
onError: (err) => {
console.error(`onError code: ${err.code}, message: ${err.message}`);
},
onReceive: (data) => {
console.info(`onReceive, data: ${JSON.stringify(data)}`);
}
}).width('80vp').height('80vp')
}
.width('100%')
}
.height('100%')
}
}
@Builder
function ColumnChild() {
Column() {
Image($r('app.media.startIcon'))
Text('test')
}
} 组件提供方
元服务提供方需要修改两个文件:
import { AbilityConstant, Want, EmbeddableUIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends EmbeddableUIAbility {
storage = new LocalStorage();
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
let mainWindow = windowStage.getMainWindowSync();
this.storage.setOrCreate('window', mainWindow);
this.storage.setOrCreate('windowStage', windowStage);
windowStage.loadContent('pages/Index', this.storage);
}
onWindowStageDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
} import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
@Entry
@Component
struct Index {
private storage: LocalStorage | undefined = this.getUIContext().getSharedLocalStorage();
build() {
Row() {
Column() {
GridRow({ columns: 2 }) {
GridCol() {
Button('setWindowSystemBar')
.onClick(() => {
this.testSetSystemBarEnable();
}).width(120)
}.height(60)
GridCol() {
Button('setGestureBack')
.onClick(() => {
this.testSetGestureBackEnable();
}).width(120)
}.height(60)
GridCol() {
Button('setImmersive')
.onClick(() => {
this.testSetImmersiveEnable();
}).width(120)
}.height(60)
GridCol() {
Button('setSpecificSystemBarEnabled')
.onClick(() => {
this.testSetSpecificSystemBarEnabled();
}).width(120)
}.height(60)
}
}
.width('100%')
}
.height('100%')
}
testSetSystemBarEnable() {
let window: window.Window | undefined = this.storage?.get('window');
let promise = window?.setWindowSystemBarEnable(['status']);
promise?.then(() => {
console.info('setWindowSystemBarEnable success');
}).catch((err: BusinessError) => {
console.error(`setWindowSystemBarEnable failed, code: ${err.code}, message: ${err.message}`);
});
}
testSetGestureBackEnable() {
let window: window.Window | undefined = this.storage?.get('window');
let promise = window?.setGestureBackEnabled(true);
promise?.then(() => {
console.info('setGestureBackEnabled success');
}).catch((err: BusinessError) => {
console.error(`setGestureBackEnabled failed, code: ${err.code}, message: ${err.message}`);
});
}
testSetImmersiveEnable() {
let window: window.Window | undefined = this.storage?.get('window');
try {
window?.setImmersiveModeEnabledState(true);
} catch (err) {
console.error(`setImmersiveModeEnabledState failed, code: ${err.code}, message: ${err.message}`);
}
}
testSetSpecificSystemBarEnabled() {
let window: window.Window | undefined = this.storage?.get('window');
let promise = window?.setSpecificSystemBarEnabled('navigationIndicator', false, false);
promise?.then(() => {
console.info('setSpecificSystemBarEnabled success');
}).catch((err: BusinessError) => {
console.error(`setSpecificSystemBarEnabled failed, code: ${err.code}, message: ${err.message}`);
});
}
}