鸿蒙开发Ability Kit程序框架服务:API切换概述 API切换

电子说

1.3w人已加入

描述

API切换概述

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

框架

startAbility接口由FA模型切换到Stage模型的示例: 开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

  • FA模型示例
    import featureAbility from '@ohos.ability.featureAbility';
    import Want from '@ohos.app.ability.Want';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PagePageAbilityFirst';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PagePageAbilityFirst {
    
      build() {
        Column() {
          List({ initialIndex: 0 }) {
            ListItem() {
              Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                //...
              }
              .onClick(() = > {
                (async (): Promise< void > = > {
                  try {
                    hilog.info(domain, TAG, 'Begin to start ability');
                    let want: Want = {
                      bundleName: 'com.samples.famodelabilitydevelop',
                      moduleName: 'entry',
                      abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton'
                    };
                    await featureAbility.startAbility({ want: want });
                    hilog.info(domain, TAG, `Start ability succeed`);
                  }
                  catch (error) {
                    hilog.error(domain, TAG, 'Start ability failed with ' + error);
                  }
                })()
              })
            }
            //...
          }
          //...
        }
        //...
      }
    }
    
  • Stage示例示例
  • HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
    框架
import hilog from '@ohos.hilog';
import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import { Caller } from '@ohos.app.ability.UIAbility';

const TAG: string = '[Page_UIAbilityComponentsInteractive]';
const DOMAIN_NUMBER: number = 0xFF00;

@Entry
@Component
struct Page_UIAbilityComponentsInteractive {
  private context = getContext(this) as common.UIAbilityContext;
  caller: Caller | undefined = undefined;
  build() {
    Column() {
      //...
      List({ initialIndex: 0 }) {
        ListItem() {
          Row() {
            //...
          }
          .onClick(() = > {
            // context为Ability对象的成员,在非Ability对象内部调用需要
            // 将Context对象传递过去
            let wantInfo: Want = {
              deviceId: '', // deviceId为空表示本设备
              bundleName: 'com.samples.stagemodelabilitydevelop',
              moduleName: 'entry', // moduleName非必选
              abilityName: 'FuncAbilityA',
              parameters: { // 自定义信息
                info: '来自EntryAbility Page_UIAbilityComponentsInteractive页面'
              },
            };
            // context为调用方UIAbility的UIAbilityContext
            this.context.startAbility(wantInfo).then(() = > {
              hilog.info(DOMAIN_NUMBER, TAG, 'startAbility success.');
            }).catch((error: BusinessError) = > {
              hilog.error(DOMAIN_NUMBER, TAG, 'startAbility failed.');
            });
          })
        }
        //...
      }
      //...
    }
    //...
  }
}

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分