一、显式拉起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}`);
}
}