HarmonyOS 鸿蒙Next 项目中打开了一系列页面,当收到一个socketio指令后,关闭当前所有页面并跳转到一个指定页面

发布于 1周前 作者 h691938207 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 项目中打开了一系列页面,当收到一个socketio指令后,关闭当前所有页面并跳转到一个指定页面

在app中打开了一系列业务页面比如A、B、C、D页面,当收到一个socketio指令后,关闭当前所有页面并跳转到一个指定登陆页面,如何实现?框架上如何设计?

2 回复

如果是关闭多个ability的话,可以调用“context.terminateSelf()”来关闭弹窗,socketio指令的具体实现可以整合到下列关键示例代码中(下列代码中使用AppStorage来模拟同步接收指令关闭ability页面): Ability1的页面

import Want from '@ohos.app.ability.Want';
import { common, UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @StorageLink('flag') @Watch('listen') flag: boolean = false

//充当socketio指令的效果
  listen() {

      if (this.flag) {
        console.log('1111111111111111')
        let context = getContext(this) as common.UIAbilityContext
        context.terminateSelf()
      }
  }


  build() {
    Column() {
      Button('前往ability2').onClick(() => {
        let wantInfo: Want = {
          deviceId: '', // deviceId为空表示本设备
          bundleName: 'com.example.ir_ability',
          abilityName: 'EntryAbility2',
        }

        let context = getContext() as common.UIAbilityContext

        const DOMAIN_NUMBER: number = 0xFF00;
        const TAG: string = '[abilityTest]';
        context.startAbility(wantInfo).then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability跳转成功')
        }).catch((error: BusinessError) => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability failed ,cause:' + error.message)
        })

      })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

Ability2页面

import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index2 {
  @State message: string = 'Hello World';
  @StorageLink('flag') @Watch('listen') flag: boolean = false

  listen() {

    if (this.flag) {
      console.log('22222222222')
      let context = getContext(this) as common.UIAbilityContext
      context.terminateSelf()
    }
  }


  build() {
    Column({ space: 5 }) {
      Text('欢迎来到ability2')
        .fontWeight(20)
        .fontColor(Color.Red)
        .fontSize(45)
      Button('前往ability3').onClick(() => {
        let wantInfo: Want = {
          deviceId: '', // deviceId为空表示本设备
          bundleName: 'com.example.ir_ability',
          abilityName: 'EntryAbility3',
        }

        let context = getContext() as common.UIAbilityContext

        const DOMAIN_NUMBER: number = 0xFF00;
        const TAG: string = '[abilityTest]';
        context.startAbility(wantInfo).then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability跳转成功')
        }).catch((error: BusinessError) => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability failed ,cause:' + error.message)
        })

      })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

Ability3页面

import { common, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index3 {
  @State message: string = 'Hello World';
  @StorageLink('flag') @Watch('listen') flag: boolean = false

  listen() {

    if (this.flag) {
      console.log('33333333333')
      let context = getContext(this) as common.UIAbilityContext
      context.terminateSelf()
    }
  }



  build() {
    Column({ space: 5 }) {
      Text('欢迎来到ability3')
        .fontWeight(20)
        .fontColor(Color.Red)
        .fontSize(45)
      Button('前往ability4').onClick(() => {
        let wantInfo: Want = {
          deviceId: '', // deviceId为空表示本设备
          bundleName: 'com.example.ir_ability',
          abilityName: 'EntryAbility4',
        }

        let context = getContext() as common.UIAbilityContext

        const DOMAIN_NUMBER: number = 0xFF00;
        const TAG: string = '[abilityTest]';
        context.startAbility(wantInfo).then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability跳转成功')
        }).catch((error: BusinessError) => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability failed ,cause:' + error.message)
        })

      })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

Ability4页面

import Want from '@ohos.app.ability.Want';
import wantConstant from '@ohos.ability.wantConstant';
import { common } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index4 {
  @State message: string = 'Hello World';
  @StorageLink('flag') @Watch('listen') flag: boolean = false

  listen() {

    if (this.flag) {
      console.log('444444444444')
      let context = getContext(this) as common.UIAbilityContext
      context.terminateSelf()
    }
  }




  build() {
    Column({space:5}) {


      Button('前往ability5').onClick(() => {


        let wantInfo: Want = {
          deviceId: '', // deviceId为空表示本设备
          bundleName: 'com.example.ir_ability',
          abilityName: 'EntryAbility5',
        }

        let context = getContext() as common.UIAbilityContext

        const DOMAIN_NUMBER: number = 0xFF00;
        const TAG: string = '[abilityTest]';
        context.startAbility(wantInfo).then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability跳转成功')
        }).catch((error: BusinessError) => {
          hilog.info(DOMAIN_NUMBER, TAG, 'Ability failed ,cause:' + error.message)
        })

        context.terminateSelf()

      })

    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }

  async implicitStartAbility() {
    try {
      let want: Want = {
        'action': wantConstant.Action.ACTION_MANAGE_APPLICATIONS_SETTINGS
      }
      let context = getContext() as common.UIAbilityContext
      await context.startAbility(want)
    } catch (e) {

    }
  }
}

Ability5页面

@Entry
@Component
struct Index5 {
  @State message: string = 'Hello World';
  @StorageLink('flag') flag: boolean = false

  onPageShow(): void {
    console.log('555555555555555555555')
    AppStorage.set('flag', true)
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('Index5HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next 项目中打开了一系列页面,当收到一个socketio指令后,关闭当前所有页面并跳转到一个指定页面的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next项目中,处理Socket.IO指令以关闭当前所有页面并跳转到指定页面的需求,可以通过以下方式实现:

  1. 监听Socket.IO指令: 在你的应用中设置一个Socket.IO监听器,当接收到特定指令时,触发页面跳转逻辑。

  2. 管理页面栈: 使用AbilityStack或相应的页面管理API来控制页面栈。当接收到Socket.IO指令时,清空当前页面栈,并启动指定页面。

  3. 页面跳转: 使用Intent启动目标页面,确保在清空页面栈后立即进行。

示例代码(伪代码):

socket.on("closeAllAndJump", (data) -> {
    // 获取页面管理器
    PageManager pageManager = getPageManager();
    
    // 清空页面栈
    pageManager.clearAbilityStack();
    
    // 创建Intent并启动目标页面
    Intent intent = new Intent();
    intent.setElement(new ElementName(yourPackageName, "com.example.TargetPage"));
    startAbility(intent);
});

请注意,上述代码为简化示例,具体实现需根据HarmonyOS的API文档和项目结构进行调整。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部