鸿蒙HarmonyOS NEXT开发实战:弱网连接迁移模式通知设置

鸿蒙开发往期必看:

HarmonyOS NEXT应用开发性能实践总结

一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!

“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到精通)

 “一杯冰美式的时间” 了解鸿蒙HarmonyOS Next应用开发路径!


场景介绍

在弱网环境下,系统发起多网迁移(WiFi<->蜂窝,主卡<->副卡等)的过程中,给应用提供连接迁移开始和完成通知,应用根据连接迁移通知的建议进行重建,快速恢复业务,给用户带来平滑、高速、低时延的上网体验。

接口说明

具体API说明详见接口文档

接口名

描述

on(type: 'handoverChange', callback: Callback<HandoverInfo>): void

订阅连接迁移。

off(type: 'handoverChange', callback?: Callback<HandoverInfo>): void

取消订阅连接迁移。

开发步骤

  1. 导入Network Boost Kit模块。
    import { netHandover } from '@kit.NetworkBoostKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. 通过订阅的方式获取连接迁移信息。
    try {
      netHandover.on('handoverChange', (info: netHandover.HandoverInfo) => {
        if (info.handoverStart) {
          // 连接迁移开始回调,应用按照HandoverStart的建议调整数传策略
          console.info('handover start');
        } else if (info.handoverComplete) {
          // 连接迁移完成回调,应用按照HandoverComplete的建议进行调速和重建恢复
          console.info('handover complete');
        }
      });
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }
  3. 当应用业务流程结束,不需要系统侧连接迁移信息时,通过去订阅的方式取消监听该信息。
    try {
      netHandover.off('handoverChange');
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }

迁移模式设置

场景介绍

应用可通过该接口变更连接迁移模式,包括委托模式由系统发起连接迁移,和自主模式由应用发起连接迁移。

接口说明

具体API说明详见接口文档

接口名

描述

setHandoverMode(mode: HandoverMode): void

应用设置迁移模式,默认为委托模式。

开发步骤

  1. 导入Network Boost Kit模块。
    import { netHandover} from '@kit.NetworkBoostKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. 调用setHandoverMode接口,设置为自主模式,禁止系统发起连接迁移。
    try{
      let mode: netHandover.HandoverMode = netHandover.HandoverMode.DISCRETION;
      netHandover.setHandoverMode(mode);
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值