最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

應用間互相跳轉并傳遞數(shù)據

2023-08-10 00:45 作者:起個名好難還被占用  | 我要投稿

參考鏈接: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



應用間互相跳轉并傳遞數(shù)據的評論 (共 條)

分享到微博請遵守國家法律
江北区| 罗定市| 朝阳市| 渭南市| 珠海市| 涡阳县| 华亭县| 温州市| 文化| 凤山县| 扎赉特旗| 开封县| 定陶县| 永寿县| 鹤岗市| 保定市| 临海市| 澎湖县| 巴南区| 关岭| 玛多县| 荆州市| 临西县| 东乌| 札达县| 南部县| 赤壁市| 澎湖县| 岱山县| 开化县| 建湖县| 惠州市| 东光县| 综艺| 安新县| 天津市| 潞西市| 清原| 岳池县| 舟山市| 北京市|