当前位置: 首页 > 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

http://www.dtcms.com/a/39047.html

相关文章:

  • 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进制
  • 【热力图 Heatmap】——1
  • NLP07-朴素贝叶斯问句分类之数据集加载(1/3)
  • 《OpenCV》——光流估计
  • 计算机基础:二进制基础01,比特与字节
  • 【最大通过数——二分】
  • vs2015下使用openmp
  • 包子凑数——蓝桥杯真题Python
  • Eclipse安装和配置环境教程包含下载、安装、汉化(附安装包)
  • 大小端存储的概念和判断
  • 2025年- G18-Lc92-169. 多数元素-java版