当前位置: 首页 > article >正文

OpenHarmony应用间跳转

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

  • 参考资料
    • 官方文档:应用间跳转概述
    • https://gitee.com/scenario-samples/pull-other-app

实现思路

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('拉起系统及三方应用')
  }
}

snapshot_20250226_172037.jpeg

本样例源码

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


http://www.kler.cn/a/567681.html

相关文章:

  • 在docker中运行R容器,并在Windows下的vscode中使用该R
  • 卷积神经网络(Convolutional Neural Network,CNN)详细解释(带示例)
  • nexus如何上传自己的依赖包
  • 工程化与框架系列(12)--响应式框架原理
  • 16.2 LangChain 表达式语言设计哲学:重新定义大模型应用开发范式
  • 4.3MISC流量分析练习-wireshark-https
  • 哈工大信息管理与信息系统本科,有C++和Python基础,如何选择就业方向?
  • taoCMS v3.0.2 任意文件读取漏洞(CVE-2022-23316)
  • 如何保证 Redis 缓存和数据库的一致性?
  • Vue3:Vue Router的学习(四)
  • 民安智库:物业满意度调查的数据分析经验分享
  • 011 rocketmq过滤消息
  • JavaWeb——HTML
  • 工程化与框架系列(13)--虚拟DOM实现
  • XML 编辑器:全面指南与最佳实践
  • 基于vue3和spring boot实现大文件上传
  • 20250225-代码笔记03-class CVRPModel AND other class
  • 备战蓝桥杯Day11 DFS
  • Leetcode1 两数之和 python两种方法实现
  • 汽车低频发射天线介绍