want的拉起回调,日常使用软件进行支付,使用当前软件唤起另外一个软件进行支付

​日常使用软件进行支付,使用当前软件唤起另外一个软件进行支付;以及一些关联软件的互通使用等等。

场景:在应用A中唤醒应用B

HarmonyOS
2024-05-22 22:55:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
li_jian_jun

使用的核心API

核心代码解释

主页代码:展示A拉起B时可以让B接受的信息。

@State message: string = 'Hello World' 
@State info: string = "project01的页面信息"; 
@State uri: string = "project01的index页面"; 
@State strArray: Array<string> = ["str1", "str1"]; 
@State strArrayObject: Array<object> = JSON.parse(JSON.stringify([{ obj1: 'aaa' }, { obj2: 100 }])); 
@State str: string = 'str1'; 
@State double: number = 1.1; 
@State flag: boolean = true; 
 
build() { 
  Column() { 
    Row() { 
      Text(this.flag ? "传给第二个应用的参数信息" : "第二个应用返回的参数信息")// .width('100%') 
        .height('100%') 
    } 
    .justifyContent(FlexAlign.Center) 
    .backgroundColor(Color.Red) 
    .width('100%') 
    .height('5%') 
    Blank() 
      .height('20%') 
    Column() { 
      Row() { 
        Text('info:') 
      } 
      Row() { 
        TextInput({ text: this.info, placeholder: "" }) 
          .onChange((value) => { 
            this.info = value 
          }) 
          .borderRadius(0) 
          .border({ width: 1, color: Color.Black }) 
      } 
      .height('40') 
 
      Row() { 
        Text('uri:') 
      } 
      Row() { 
        TextInput({ text: this.uri, placeholder: "" }) 
          .onChange((value) => { 
            this.uri = value 
          }) 
          .borderRadius(0) 
          .border({ width: 1, color: Color.Black }) 
      } 
      .height('40') 
 
      Row() { 
        Text('str:') 
      } 
 
      Row() { 
        TextInput({ text: this.str, placeholder: "" }) 
          .onChange((value) => { 
            this.str = value 
          }) 
          .borderRadius(0) 
          .border({ width: 1, color: Color.Black }) 
      } 
      .height('40') 
 
      Row() { 
        Text('double:') 
      } 
 
      Row() { 
        TextInput({ text: this.double + "", placeholder: "" }) 
          .onChange((value) => { 
            this.double = JSON.parse(JSON.stringify(value)) 
          }) 
          .borderRadius(0) 
          .border({ width: 1, color: Color.Black }) 
      } 
      .height('40') 
 
      Row() { 
        Text('strArray:') 
      } 
 
      Row() { 
        TextInput({ text: JSON.stringify(this.strArray), placeholder: "" }) 
          .onChange((value) => { 
            this.strArray = JSON.parse(JSON.stringify(value)) 
          }) 
          .borderRadius(0) 
          .border({ width: 1, color: Color.Black }) 
      } 
      .height('40') 
 
      Row() { 
        Text('strArrayObject:') 
      } 
 
      Row() { 
        TextInput({ text: JSON.stringify(this.strArrayObject), placeholder: "" }) 
          .onChange((value) => { 
            this.strArrayObject = JSON.parse(JSON.stringify(value)) 
          }) 
          .borderRadius(0) 
          .border({ width: 1, color: Color.Black }) 
      } 
      .height('40') 
 
    } 
    .justifyContent(FlexAlign.Center) 
    .height('50%') 
    .backgroundColor(Color.Yellow) 
    .width('80%') 
 
    Blank() 
      .height('20') 
    Row() { 
      Button("拉起页面", { type: ButtonType.Normal, stateEffect: true }) 
        .borderRadius(100) 
        .width(200) 
        .height(40) 
        .onClick(() => { 
          this.setWantInfo() 
        }) 
    } 
    .height('10%') 
    .width('80%') 
    .justifyContent(FlexAlign.Center) 
 
    Row() { 
      Button("返回主页", { type: ButtonType.Normal, stateEffect: true }) 
        .borderRadius(100) 
        .width(200) 
        .height(40) 
        .onClick(() => { 
          router.pushUrl({ url: "pages/Index" }) 
        }) 
    } 
    .height('10%') 
    .width('80%') 
    .justifyContent(FlexAlign.Center) 
 
  } 
  .width('100%') 
  .height('100%') 
}

提供点击事件,调用startAbilityForResult()接口启动拉起B应用,保存B应用回调的信息。

setWantInfo() { 
  let want: Want = { 
    deviceId: '', // deviceId为空表示本设备 
    bundleName: 'com.example.project02', 
    abilityName: 'EntryAbility', 
    // action:"ohos.want.action.sendData", 
    uri: this.uri, 
    parameters: { 
      info: this.info, 
      keyString: this.str, 
      keyDouble: this.double, 
      keyArray: this.strArray, 
      keyArrayObject: this.strArrayObject, 
    } 
  } 
 
  context.startAbilityForResult(want).then((data) => { 
    this.str = JSON.parse(JSON.stringify(data.want?.parameters?.keyForString)) 
    this.info = JSON.parse(JSON.stringify(data.want?.parameters?.info)) 
    this.uri = JSON.parse(JSON.stringify(data.want?.uri)); 
    this.double = JSON.parse(JSON.stringify(data.want?.parameters?.keyForDouble)); 
    this.strArray = JSON.parse(JSON.stringify(data.want?.parameters?.keyForArray)) 
    this.strArrayObject = JSON.parse(JSON.stringify(data.want?.parameters?.keyForArrayObject)); 
    this.flag = JSON.parse(JSON.stringify(data.want?.parameters?.keyForBool)); 
    console.info('Succeeded in starting ability.'); 
  }).catch((err: BusinessError) => { 
    console.error(`Failed to start ability. Code is ${err.code}, message is ${err.message}`); 
  }) 
} 
拉起的B应用接收的信息: 
在B应用中EntryAbility文件中onCreate()或者onNewWant()生命周期回调文件中接收EntryAbility传递过来的参数 
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam) { 
  let funcAbilityWant = want; 
  AppStorage.set('want', funcAbilityWant); 
  console.log(`onNewWant, want: ${want.abilityName}`); 
  console.log(`onNewWant, launchParam: ${JSON.stringify(launchParam)}`); 
}
分享
微博
QQ
微信
回复
2024-05-23 17:15:22
相关问题
支付成功后没有收到
470浏览 • 1回复 待解决
软件下载问题 软件下载问题!?!
4373浏览 • 1回复 待解决
鸿蒙如何开发工业软件
5911浏览 • 2回复 待解决
使用华为支付示例代码吗
423浏览 • 1回复 待解决
编程烧录,需要什么软件?
2629浏览 • 1回复 待解决
有哪些好用MySQL监控软件
874浏览 • 1回复 待解决
DevEco Studio软件UI怎么设置
5250浏览 • 1回复 待解决
鸿蒙TextField 软件盘不弹出
4427浏览 • 2回复 待解决
Linux软件源问题有懂吗?
861浏览 • 1回复 待解决
如何实现软件退出需要锁屏密码?
1106浏览 • 1回复 待解决
网络请求后如何进行
565浏览 • 1回复 待解决
Linux下有什么好Git服务器软件?
779浏览 • 1回复 待解决