HarmonyOS实战开发-应用间的跳转(两种方法)

一、显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability

import { router } from '@kit.ArkUI';
import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
import { AbilityConstant, common, UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { wantAgent, WantAgent } from '@kit.AbilityKit';
import { audio } from '@kit.AudioKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'AudioCapturerDemo';


class Data {
  name: string
  constructor(name: string) {
    this.name = name
  }
}

const datas = [
  new Data("4")
]


@Entry
@Component
struct MainPage {
  @State message1: string = '按哪个按钮?都不好使';
  @State message: string = 'ContinuousTask';
  // 通过getContext方法,来获取page所在的UIAbility上下文。
  private context: Context = getContext(this);

  build() {
    RelativeContainer() {

      Text(this.message1)
        .id('HelloWorld')
        .height(70)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Column() {
        List({ space : 15 }) {
          ForEach(datas, (item: Data) => {
            ListItem() {
              Row() {
                Text(item.name)
                  .fontColor(Color.Black)
                  .fontSize(22)
                  .width('80%')
                  .alignRules({
                    left: { anchor: 'listItem', align: HorizontalAlign.Start }
                  })


                Image($r("app.media.go"))
                  .width(30)
                  .height(30)
                  .alignRules({
                    right: { anchor: 'listItem', align: HorizontalAlign.End }
                  })
              }
              .id('listItem')
              .width('100%')
              .height(80)
              .margin({ left: 20, right: 20 })
            }
            .backgroundColor('#fff')
            .onClick(() => {
              if (item.name === '4') {
                lcs();
                console.info('lcs')
              }

            })
          })
        }
        .margin({ top: 20 })
      }
      .height('100%')
      .backgroundColor('#ff9b9696')
      .margin({ top: 10 })
      .alignRules({
        top: { anchor: 'HelloWorld', align: VerticalAlign.Bottom },
      })
    }.width('100%').height('100%')
  }
}

const TAG1: string = '[UIAbilityComponentsOpenLink]';
const DOMAIN_NUMBER: number = 0xFF00;

function lcs() {
   let wantInfo: Want = {
     deviceId: '', // deviceId为空表示本设备
     bundleName: '目标的bundleName,在AppScope/app.json5中',
     moduleName: '模块名', // moduleName非必选
     abilityName: '主ability',
   };
  let want: Want = {
    uri: "link://zd.lcs.lcs"
  };

  context.startAbility(wantInfo, (err: BusinessError) => {
    if (err.code) {
      显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability
      console.error(`Failed to startAbility. Code: ${err.code}, message: ${err.message}`);
     }
   });
}

 

 二、通过调用隐式want匹配的方法触发应用跳转

通过startAbility接口启动时,还需要自己传入待匹配的action和entity。

src/main/module.json5中加入

{
  "module": {
    // ...
    "abilities": [
      {
        // ...
        "skills": [
          {
            "uris": [
              {
                // scheme必选,可以自定义,以link为例,需要替换为实际的scheme
                "scheme": "link",
                // host必选,配置待匹配的域名
                "host": "www.example.com",
                // path可选,为了避免匹配到多个应用,建议配置该字段
                "path": "path1"
              }
            ]
          }
        ]
      }
    ]
  }
}
import { router } from '@kit.ArkUI';
import { backgroundTaskManager } from '@kit.BackgroundTasksKit';
import { AbilityConstant, common, UIAbility, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { wantAgent, WantAgent } from '@kit.AbilityKit';
import { audio } from '@kit.AudioKit';
import { fileIo } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG = 'AudioCapturerDemo';


class Data {
  name: string
  constructor(name: string) {
    this.name = name
  }
}

const datas = [
  new Data("4")
]


@Entry
@Component
struct MainPage {
  @State message1: string = '按哪个按钮?都不好使';
  @State message: string = 'ContinuousTask';
  // 通过getContext方法,来获取page所在的UIAbility上下文。
  private context: Context = getContext(this);

  build() {
    RelativeContainer() {

      Text(this.message1)
        .id('HelloWorld')
        .height(70)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Column() {
        List({ space : 15 }) {
          ForEach(datas, (item: Data) => {
            ListItem() {
              Row() {
                Text(item.name)
                  .fontColor(Color.Black)
                  .fontSize(22)
                  .width('80%')
                  .alignRules({
                    left: { anchor: 'listItem', align: HorizontalAlign.Start }
                  })


                Image($r("app.media.go"))
                  .width(30)
                  .height(30)
                  .alignRules({
                    right: { anchor: 'listItem', align: HorizontalAlign.End }
                  })
              }
              .id('listItem')
              .width('100%')
              .height(80)
              .margin({ left: 20, right: 20 })
            }
            .backgroundColor('#fff')
            .onClick(() => {
              if (item.name === '4') {
                lcs();
                console.info('lcs')
              }

            })
          })
        }
        .margin({ top: 20 })
      }
      .height('100%')
      .backgroundColor('#ff9b9696')
      .margin({ top: 10 })
      .alignRules({
        top: { anchor: 'HelloWorld', align: VerticalAlign.Bottom },
      })
    }.width('100%').height('100%')
  }
}

const TAG1: string = '[UIAbilityComponentsOpenLink]';
const DOMAIN_NUMBER: number = 0xFF00;

function lcs() {
  let want: Want = {
    uri: "link://zd.lcs.lcs"
  };

  try {
    context.startAbility(want).then(() => {
      hilog.info(DOMAIN_NUMBER, TAG1, 'start ability success.');
    }).catch((err: BusinessError) => {
      hilog.error(DOMAIN_NUMBER, TAG1, `start ability failed. Code is ${err.code}, message is ${err.message}`);
    })
  } catch (paramError) {
    hilog.error(DOMAIN_NUMBER, TAG1, `Failed to start ability. Code is ${paramError.code}, message is ${paramError.message}`);
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L.2626

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值