harmony 鸿蒙API切换概述

  • 2022-12-13
  • 浏览 (932)

API切换概述

FA模型和Stage模型由于线程模型和进程模型的差异,部分接口仅在FA模型下才能使用,针对这部分接口在SDK的接口中有FAModelOnly的标记,用于提醒开发者这部分接口仅能在FA模型下使用。因此在切换到Stage模型时,需要将应用中用到的FAModelOnly接口替换成Stage模型下对应的接口。下面是startAbility的接口切换示例,全量接口列表请查看后续章节:

api-switch-overview

startAbility接口由FA模型切换到Stage模型的示例:

  • FA模型示例
  import fa from '@ohos.ability.featureAbility';
  import { BusinessError } from '@ohos.base';

  fa.startAbility({
    "want": {
      bundleName: "com.example.myapplication",
      abilityName: "com.example.myapplication.EntryAbility"
    }
  }).then((data) => {
    console.info('startAbility success');
  }).catch((error: BusinessError) => {
    console.error('startAbility failed.');
  })
  • Stage示例示例
  import Want from '@ohos.app.ability.Want';

  // context为Ability对象的成员,在非Ability对象内部调用需要
  // 将Context对象传递过去
  let wantInfo: Want = {
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
  };
  this.context.startAbility(wantInfo).then(() => {
    console.info('startAbility success.');
  }).catch((error: BusinessError) => {
    console.error('startAbility failed.');
  })

你可能感兴趣的鸿蒙文章

harmony 鸿蒙应用模型

harmony 鸿蒙使用显式Want启动应用组件

harmony 鸿蒙使用隐式Want打开网址

harmony 鸿蒙AbilityStage组件容器

harmony 鸿蒙访问DataAbility

harmony 鸿蒙FA模型访问Stage模型DataShareExtensionAbility

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙常见action与entities

harmony 鸿蒙app和deviceConfig的切换

harmony 鸿蒙应用/组件级配置

0  赞