應用間互相跳轉并傳遞數(shù)據
參考鏈接:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-models/uiability-intra-device-interaction.md#%E5%90%AF%E5%8A%A8%E5%85%B6%E4%BB%96%E5%BA%94%E7%94%A8%E7%9A%84uiability
let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext
let want = {
?deviceId: '', // deviceId為空表示本設備
?bundleName: 'com.example.projectb',
?moduleName: 'entry', // moduleName非必選
?abilityName: 'EntryAbility',
?parameters: { // 自定義信息
? ?numa: 1,
? ?numb: 2
?},
}
// context為調用方UIAbility的UIAbilityContext
context.startAbility(want).then(() => {
?console.info('Succeeded in starting ability.');
}).catch((err) => {
?console.error(`Failed to start ability. Code is ${err.code}, message is ${err.message}`);
})
步驟:1. 構建want參數(shù),要跳轉對應應用的bundleName和moduleName
????????????2. 調用context.startAbility方法跳轉到對應應用
ProjectA
// index.ets
import UIAbility from '@ohos.app.ability.UIAbility'
import common from '@ohos.app.ability.common';
@Entry
@Component
struct Index {
?@State message: string = 'ProjectA'
?@StorageLink('sum') sum: number = 0;
?build() {
? ?Row() {
? ? ?Column() {
? ? ? ?Text(this.message)
? ? ? ? ?.fontSize(50)
? ? ? ? ?.fontWeight(FontWeight.Bold)
? ? ? ?Button('跳轉ProjectB')
? ? ? ? ?.onClick(() => {
? ? ? ? ? ?let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext
? ? ? ? ? ?let want = {
? ? ? ? ? ? ?deviceId: '', // deviceId為空表示本設備
? ? ? ? ? ? ?bundleName: 'com.example.projectb',
? ? ? ? ? ? ?moduleName: 'entry', // moduleName非必選
? ? ? ? ? ? ?abilityName: 'EntryAbility',
? ? ? ? ? ? ?parameters: { // 自定義信息
? ? ? ? ? ? ? ?numa: 1,
? ? ? ? ? ? ? ?numb: 2
? ? ? ? ? ? ?},
? ? ? ? ? ?}
? ? ? ? ? ?// context為調用方UIAbility的UIAbilityContext
? ? ? ? ? ?context.startAbility(want).then(() => {
? ? ? ? ? ? ?console.info('Succeeded in starting ability.');
? ? ? ? ? ?}).catch((err) => {
? ? ? ? ? ? ?console.error(`Failed to start ability. Code is ${err.code}, message is ${err.message}`);
? ? ? ? ? ?})
? ? ? ? ?})
? ? ? ?Text('從ProjectB拿到返回值' + this.sum)
? ? ?}
? ? ?.width('100%')
? ?}
? ?.height('100%')
?}
}//?EntryAbility.ts
onCreate(want, launchParam) {
?hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
?console.log('onNewWant');
?let funcAbilityWant = want;
?let sum = funcAbilityWant?.parameters?.sum;
?AppStorage.SetOrCreate('sum', sum);
}
onNewWant(want) {
?console.log('onNewWant');
?let funcAbilityWant = want;
?let sum = funcAbilityWant?.parameters?.sum;
?AppStorage.SetOrCreate('sum', sum);
}
ProjectB
// index.ets
import common from '@ohos.app.ability.common'
@Entry
@Component
struct Index {
?@State message: string = 'projectB'
?build() {
? ?Row() {
? ? ?Column() {
? ? ? ?Text(this.message)
? ? ? ? ?.fontSize(50)
? ? ? ? ?.fontWeight(FontWeight.Bold)
? ? ? ?Button('跳轉ProjectA')
? ? ? ? ?.onClick(() => {
? ? ? ? ? ?let context = getContext(this) as common.UIAbilityContext; // UIAbilityContext
? ? ? ? ? ?let want = {
? ? ? ? ? ? ?deviceId: '', // deviceId為空表示本設備
? ? ? ? ? ? ?bundleName: 'com.example.projecta',
? ? ? ? ? ? ?moduleName: 'entry', // moduleName非必選
? ? ? ? ? ? ?abilityName: 'EntryAbility',
? ? ? ? ? ? ?parameters: { // 自定義信息
? ? ? ? ? ? ? ?sum: AppStorage.Get('sum')
? ? ? ? ? ? ?},
? ? ? ? ? ?}
? ? ? ? ? ?// context為調用方UIAbility的UIAbilityContext
? ? ? ? ? ?context.startAbility(want).then(() => {
? ? ? ? ? ? ?console.info('Succeeded in starting ability.');
? ? ? ? ? ?}).catch((err) => {
? ? ? ? ? ? ?console.error(`Failed to start ability. Code is ${err.code}, message is ${err.message}`);
? ? ? ? ? ?})
? ? ? ? ?})
? ? ?}
? ? ?.width('100%')
? ?}
? ?.height('100%')
?}
}// EntryAbility.ts
onCreate(want, launchParam) {
?hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
?let funcAbilityWant = want;
?let numa = funcAbilityWant?.parameters?.numa;
?let numb = funcAbilityWant?.parameters?.numb;
?console.log('testTwoApp' + numa + numb);
?let sum = numa + numb;
?AppStorage.SetOrCreate('sum', sum);
}
onNewWant(want) {
?console.log('onNewWant');
?let funcAbilityWant = want;
?let numa = funcAbilityWant?.parameters?.numa;
?let numb = funcAbilityWant?.parameters?.numb;
?console.log('testTwoApp' + numa + numb);
?let sum = numa + numb;
?AppStorage.SetOrCreate('sum', sum);
}
參考視頻:https://www.bilibili.com/video/BV1AV411V7yn/?vd_source=ca38750c3d4234b7d9d5f8fd31f855d1