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

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

相关文章:

  • Lua的table(表)
  • JSON Schema 入门指南:如何定义和验证 JSON 数据结构
  • QT和有道词典有冲突,导致内存溢出,闪退。
  • neo4j 和 langchain_community.graphs.Neo4jGraph 对比
  • 前缀和专题练习 ——基于罗勇军老师的《蓝桥杯算法入门C/C++》
  • 机试刷题_NC17 最长回文子串【python】
  • PostgreSQL:模拟插入数据和查询(带时间)
  • 三轴加速度推算姿态角的方法,理论分析和MATLAB例程
  • Leetcode3162:优质数对的总数 I
  • Spring Boot @Async 注解深度指南
  • 湘潭大学计算机复试详细攻略(调剂)
  • Ubuntu 下 nginx-1.24.0 源码分析 - NGX_CYCLE_POOL_SIZE 宏
  • 智能生活综合平台需求规格说明书
  • 弱监督语义分割学习计划(1)-简单实现CAM但是效果不好
  • wordpress按不同页调用不同的标题3种形式
  • AOP进阶-02.通知顺序
  • 上传securecmd失败
  • 【万字长文】开源之播对话白鲸开源CEO郭炜--乐观主义的开源精神走得更远
  • 【Web安全】图片验证码DOS漏洞
  • C# tostring 转换成16进制
  • 中方是否认同俄方关于新纳粹主义观点?外交部:联大曾多次通过相关决议
  • 阿里CEO:将以饱和式投入打法,聚焦几大核心战役
  • 重温经典|《南郭先生》:不模仿别人,不重复自己
  • 近4小时会谈、3项联合声明、20多份双边合作文本,中俄元首今年首次面对面会晤成果颇丰
  • 吴清稳市场稳预期发布会十要点:谈平准基金、股市稳定、公募改革和巴菲特
  • 央行行长详解降准:将释放长期流动性1万亿,整体存款准备金率平均水平降至6.2%