目录

应用拉起元服务

问题现象

应用如何拉起开发者自身或三方开发的元服务?

背景知识

解决方案

不建议使用下面的方式打开元服务:

拉起方式

原因

通过FunctionalButton拉起元服务。

FunctionalButton组件打开元服务需要获取未开放的信息,增加开发成本。

通过startAbility/startAbilityForResult拉起元服务。

使用该接口打开元服务需要获取未开放的信息,增加开发成本。

完整示例参考如下:

import { FullScreenLaunchComponent, HalfScreenLaunchComponent } from '@kit.ArkUI';
import { AtomicServiceOptions, common, Want, wantConstant } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { FunctionalButton, functionalButtonComponentManager } from '@kit.ScenarioFusionKit';

@Entry
@Component
struct AppLaunchAtomicService {
  build() {
    Navigation() {
      Column({ space: 40 }) {
        ByFullScreenLaunch();
        ByOpenAtomicService();
        ByOpenLink();
        ByFunctionalButton();
        ByStartAbility();
        ByHalfScreenLaunch();
      }
      .height('100%')
      .width('100%')
      .justifyContent(FlexAlign.Center);
    }
    .title('应用拉起元服务')
    .hideBackButton(true)
    .titleMode(NavigationTitleMode.Mini)
    .mode(NavigationMode.Stack);
  }
}

@Component
struct ByFullScreenLaunch {
  @Builder
  PhoneRechargeChild() {
    Column() {
      Button('ByFullScreenLaunch')
        .fontSize(16)
        .width(200)
    };
  }

  build() {
    Column() {
      FullScreenLaunchComponent({
        content: this.PhoneRechargeChild,
        appId: '****', // 请替换为待拉起元服务的真实appId
        options: {},
      });
    };
  }
}

@Component
struct ByOpenAtomicService {
  build() {
    Column() {
      Button('ByOpenAtomicService')
        .fontSize(16)
        .width(200)
        .onClick(() => {
          let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
          let appId: string = '****'; // 请替换为待拉起元服务的真实appId
          let options: AtomicServiceOptions = {
            flags: wantConstant.Flags.FLAG_INSTALL_ON_DEMAND
          };
          try {
            context.openAtomicService(appId, options)
              .then((result: common.AbilityResult) => {
                // 执行正常业务
                console.info(`openAtomicService succeed, resultCode: ${result.resultCode}`);
              })
              .catch((err: BusinessError) => {
                // 处理业务逻辑错误
                console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`);
              });
          } catch (err) {
            // 处理入参错误异常
            let code = (err as BusinessError).code;
            let message = (err as BusinessError).message;
            console.error(`openAtomicService failed, code is ${code}, message is ${message}`);
          }
        });
    };
  }
}

@Component
struct ByOpenLink {
  build() {
    Column() {
      Button('ByOpenLink')
        .fontSize(16)
        .width(200)
        .onClick(() => {
          let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
          // 请替换成真实元服务链接
          let link: string = '****';
          context.openLink(link)
            .then(() => {
              // 执行正常业务
              console.info('openAtomicService succeed');
            })
            .catch((err: BusinessError) => {
              // 处理业务逻辑错误
              console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`);
            });
        });
    };
  }
}

@Component
struct ByFunctionalButton {
  build() {
    Column() {
      FunctionalButton({
        params: {
          openType: functionalButtonComponentManager.OpenType.LAUNCH_APP,
          label: 'ByFunctionalButton',

          appParam: {
            bundleName: '****', // 请替换为待拉起元服务真实bundleName
            abilityName: '****', // 请替换为待拉起元服务真实abilityName
          },
          styleOption: {
            styleConfig: new functionalButtonComponentManager.ButtonConfig()
              .fontSize(16)
              .width(200)
          },
        },
        controller: new functionalButtonComponentManager.FunctionalButtonController().onLaunchApp((err) => {
          if (err) {
            // 异常处理
            return;
          }
        })
      });
    };
  }
}

@Component
struct ByStartAbility {
  build() {
    Column() {
      Button('ByStartAbility')
        .fontSize(16)
        .width(200)
        .onClick(() => {
          let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
          let want: Want = {
            bundleName: '****', // 请替换为待拉起元服务真实bundleName
            abilityName: '****', // 请替换为待拉起元服务真实abilityName
          };

          try {
            context.startAbility(want, (err: BusinessError) => {
              if (err.code) {
                // 处理业务逻辑错误
                console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
                return;
              }
              // 执行正常业务
              console.info('startAbility succeed');
            });
          } catch (err) {
            // 处理入参错误异常
            let code = (err as BusinessError).code;
            let message = (err as BusinessError).message;
            console.error(`startAbility failed, code is ${code}, message is ${message}`);
          }
        });
    };
  }
}

@Component
struct ByHalfScreenLaunch {
  build() {
    Column() {
      HalfScreenLaunchComponent({
        appId: '****', // 请替换为待拉起元服务的真实appId
        options: {},
        onTerminated: (info: TerminationInfo) => {
          console.info('onTerminated info = ' + info.want);
        },
        onError: (err) => {
          console.error(' onError code: ' + err.code + ', message: ', err.message);
        },
      }) {
        Column() {
          Button('ByHalfScreenLaunch')
            .fontSize(16)
            .width(200)
        };
      };
    };
  }
}

常见FAQ

Q:全屏嵌入式和跳出式运行元服务区别是什么?

A:全屏嵌入式运行元服务是指,通过UIExtensionComponent的组件方式嵌入到使用方的组件树中,拉起EmbeddableUIAbility,展示提供方的应用内容。能够实现组件式的交互体验。跳出式运行元服务是指,非组件化的方式拉起EmbeddableUIAbility,交互体验接近独立窗口。

Q:元服务是否支持跳转应用?

A:如果被拉起方是非关联主体的三方应用,则无法拉起。如果被拉起方是关联主体账号组的三方应用,则可以拉起。详情请参考跳转规则说明。