鸿蒙开发Ability Kit程序框架服务:FA模型绑定Stage模型ServiceExtensionAbility

电子说

1.3w人已加入

描述

FA模型绑定Stage模型ServiceExtensionAbility

本文介绍FA模型的三种应用组件如何绑定Stage模型的ServiceExtensionAbility组件。
开发前请熟悉鸿蒙开发指导文档 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

PageAbility关联访问ServiceExtensionAbility

PageAbility关联访问ServiceExtensionAbility和PageAbility关联访问ServiceAbility的方式完全相同。

import featureAbility from '@ohos.ability.featureAbility';
import common from '@ohos.app.ability.common';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import hilog from '@ohos.hilog';

const TAG: string = 'PageInterflowFaAndStage';
const domain: number = 0xFF00;

@Entry
@Component
struct PageInterflowFaAndStage {
  build() {
    Column() {
      // ...
      List({ initialIndex: 0 }) {
        ListItem() {
          Row() {
            // ...
          }
          .onClick(() = > {
            let want: Want = {
              bundleName: 'ohos.samples.etsclock',
              abilityName: 'MainAbility'
            };
            featureAbility.startAbility({ want }).then((code) = > {
              hilog.info(domain, TAG, 'Ability verify code: ' + JSON.stringify(code));
            }).catch((error: BusinessError) = > {
              hilog.error(domain, TAG, 'Ability failed: ' + JSON.stringify(error));
            });
            let serviceWant: Want = {
              bundleName: 'com.samples.stagemodelabilityinteraction',
              abilityName: 'ServiceExtAbility'
            };
            let faConnect: common.ConnectOptions = {
              onConnect: (elementName, proxy) = > {
                hilog.info(domain, TAG, "FaConnection onConnect called.");
              },
              onDisconnect: (elementName) = > {
                hilog.info(domain, TAG, "FaConnection onDisconnect called.");
              },
              onFailed: (code) = > {
                hilog.info(domain, TAG, "FaConnection onFailed code is: " + code);
              }
            };
            let connectionId = featureAbility.connectAbility(serviceWant, faConnect);
          })
        }
        // ...
      }
      // ...
    }
    // ...
  }
}

ServiceAbility/DataAbility关联访问ServiceExtensionAbility

ServiceAbility/DataAbility关联访问ServiceExtensionAbility和ServiceAbility/DataAbility关联访问ServiceAbility的方式完全相同。
HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿

鸿蒙

import type common from '@ohos.app.ability.common';
import particleAbility from '@ohos.ability.particleAbility';
import type Want from '@ohos.app.ability.Want';
import type { BusinessError } from '@ohos.base';
import hilog from '@ohos.hilog';

const TAG: string = '[Sample_FAModelAbilityDevelop]';
const domain: number = 0xFF00;

class ServiceAbilityStartUiAbility {
  onStart(): void {
    // 启动UIAbility
    let want: Want = {
      bundleName: 'ohos.samples.etsclock',
      abilityName: 'MainAbility'
    };
    particleAbility.startAbility({ want }).then(() = > {
      hilog.info(domain, TAG, 'ServiceAbilityStartUIAbility Start Ability successfully.');
    }).catch((error: BusinessError) = > {
      hilog.info(domain, TAG, 'ServiceAbilityStartUIAbility Ability failed: ' + JSON.stringify(error));
    });

    // 访问ServiceExtensionAbility
    let serviceWant: Want = {
      bundleName: 'com.samples.stagemodelabilityinteraction',
      abilityName: 'ServiceExtAbility'
    };
    let faConnect: common.ConnectOptions = {
      onConnect: (elementName, proxy) = > {
        hilog.info(domain, TAG, 'FaConnection onConnect called.');
      },
      onDisconnect: (elementName) = > {
        hilog.info(domain, TAG, 'FaConnection onDisconnect called.');
      },
      onFailed: (code) = > {
        hilog.info(domain, TAG, 'FaConnection onFailed code is: ' + code);
      }
    };
    let connectionId = particleAbility.connectAbility(serviceWant, faConnect);
    hilog.info(domain, TAG, 'ServiceAbilityStartUIAbility ServiceAbility onStart');
  }
};

export default new ServiceAbilityStartUiAbility();

审核编辑 黄宇

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

全部0条评论

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

×
20
完善资料,
赚取积分