OpenHarmony应用间跳转 原创

离北况归
发布于 2025-2-26 17:49
1.8w浏览
0收藏

在开发OpenHarmony的时候,经常遇到需要把所有测试功能的hap做成一个hap的需求。显然如果一个hap内集成所有测试hap的方案需要花费较大时间成本且各个测试hap之间api并不统一。这里采用应用间跳转的方式。

实现思路

1.这里采用指定Ability方式(即显式Want)拉起其他应用方式。

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

@Entry
@Component
struct Index {
  context = getContext(this) as common.UIAbilityContext;
  navPathStack: NavPathStack = new NavPathStack();

  @Styles
  commonButton() {
    .width(250)
    .margin(5)
  }

  build() {
    Navigation(this.navPathStack) {
      Column() {
        Text('通过startAbility拉起:')

        Button('跳转系统相机')
          .onClick(() => {
            let want: Want = {
              bundleName: 'com.ohos.camera',
              abilityName: 'com.ohos.camera.MainAbility',
            };
            this.context.startAbilityForResult(want).then((data) => {
              hilog.info(0x0000, 'Success', JSON.stringify(data))
            }).catch(() => {
              hilog.info(0x0000, 'error', '')
            })
          })
          .commonButton()

        Button('跳转系统设置wifi页面')
          .onClick(() => {
            let want: Want = {
              bundleName: 'com.ohos.settings',
              abilityName: 'com.ohos.settings.MainAbility',
              uri: 'wifi', // 对应wifi设置页,不传就跳转系统设置首页/当前所在页
            };
            this.context.startAbility(want).then(() => {
            }).catch((err: BusinessError) => {
              hilog.error(0x0000, 'Failed to startAbility. Code:', `${err.code}${err.message}`);
            });
          })
          .commonButton()

        Button('跳转系统设置蓝牙页面')
          .onClick(() => {
            let want: Want = {
              bundleName: 'com.ohos.settings',
              abilityName: 'com.ohos.settings.MainAbility',
              uri: 'bluetooth', // 对应wifi设置页,不传就跳转系统设置首页/当前所在页
            };
            this.context.startAbility(want).then(() => {
            }).catch((err: BusinessError) => {
              hilog.error(0x0000, 'Failed to startAbility. Code:', `${err.code}${err.message}`);
            });
          })
          .commonButton()

        Button('跳转系统音乐应用')
          .onClick(() => {
            let want: Want = {
              bundleName: 'ohos.samples.distributedmusicplayer',
              abilityName: 'ohos.samples.distributedmusicplayer.MainAbility',
              uri: 'bluetooth', // 对应wifi设置页,不传就跳转系统设置首页/当前所在页
            };
            this.context.startAbility(want).then(() => {
            }).catch((err: BusinessError) => {
              hilog.error(0x0000, 'Failed to startAbility. Code:', `${err.code}${err.message}`);
            });
          })
          .commonButton()

      }
      .width('100%')
      .height('100%')
    }
    .title('拉起系统及三方应用')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.

OpenHarmony应用间跳转-鸿蒙开发者社区

本样例源码

https://gitee.com/from-north-to-north/OpenHarmony_p7885/blob/master/hap/easy_demo/demo_tiaozhuan/README.md

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏
回复
举报


回复
    相关推荐