鸿蒙开发往期必看:
一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!
“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到精通)
“一杯冰美式的时间” 了解鸿蒙HarmonyOS Next应用开发路径!
场景介绍
在弱网环境下,系统发起多网迁移(WiFi<->蜂窝,主卡<->副卡等)的过程中,给应用提供连接迁移开始和完成通知,应用根据连接迁移通知的建议进行重建,快速恢复业务,给用户带来平滑、高速、低时延的上网体验。
接口说明
具体API说明详见接口文档。
接口名 | 描述 |
---|---|
on(type: 'handoverChange', callback: Callback<HandoverInfo>): void | 订阅连接迁移。 |
off(type: 'handoverChange', callback?: Callback<HandoverInfo>): void | 取消订阅连接迁移。 |
开发步骤
- 导入Network Boost Kit模块。
import { netHandover } from '@kit.NetworkBoostKit'; import { BusinessError } from '@kit.BasicServicesKit';
- 通过订阅的方式获取连接迁移信息。
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); }
- 当应用业务流程结束,不需要系统侧连接迁移信息时,通过去订阅的方式取消监听该信息。
try { netHandover.off('handoverChange'); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); }
迁移模式设置
场景介绍
应用可通过该接口变更连接迁移模式,包括委托模式由系统发起连接迁移,和自主模式由应用发起连接迁移。
接口说明
具体API说明详见接口文档。
接口名 | 描述 |
---|---|
setHandoverMode(mode: HandoverMode): void | 应用设置迁移模式,默认为委托模式。 |
开发步骤
- 导入Network Boost Kit模块。
import { netHandover} from '@kit.NetworkBoostKit'; import { BusinessError } from '@kit.BasicServicesKit';
- 调用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); }