如何判断当前网络能否上网

如何判断当前网络能否上网

HarmonyOS
2024-07-23 11:28:38
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
人提唱盘

可以使用@ohos.net.connection的能力,每次网络连接发生变化时,都判断一下当前网络是否能上网,然后将判断结果用AppStorage存储起来,需要判断能否上网时直接从AppStorage里获取结果,示例代码如下:

import { connection } from '@kit.NetworkKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
export class NetJudge{ 
  public static conn:connection.NetConnection|undefined= undefined; 
 
  private static JUDGE_NET_TAG:string = 'NetJudge.currNet.isUseful'; 
 
  public static netFlag:string = 'false' 
 
  private  static init(){ 
    NetJudge.conn = connection.createNetConnection(); 
    NetJudge.conn.register(()=>{ 
      console.info('connection register success') 
    }) 
 
    NetJudge.conn.on('netAvailable',(data)=>{ 
      console.info('NetJudge  netAvailable ') 
      AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet()) 
    }) 
 
    NetJudge.conn.on('netUnavailable',()=>{ 
      console.info('NetJudge  netUnavailable ') 
      AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet()) 
    }) 
 
    NetJudge.conn.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => { 
      AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet()) 
    }) 
 
    // 订阅网络连接信息变化事件。调用register后,才能接收到此事件通知 
    NetJudge.conn.on('netConnectionPropertiesChange', (data: connection.NetConnectionPropertyInfo) => { 
      AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet()) 
    }); 
 
    NetJudge.conn.on('netLost',()=>{ 
      AppStorage.setOrCreate(NetJudge.JUDGE_NET_TAG, NetJudge.judgeHasNet()) 
    }) 
 
  } 
 
  public static regist(){ 
    if(NetJudge.conn == undefined){ 
      NetJudge.init() 
    } 
  } 
 
  public static judgeHasNet(): boolean { 
    try { // 获取当前网络连接 
      let netHandle = connection.getDefaultNetSync() 
 
      // 0-100 为系统预留的连接 
      if (!netHandle || netHandle.netId < 100) { 
        return false; 
      } 
 
      // 获取连接的属性 
      let netCapa = connection.getNetCapabilitiesSync(netHandle); 
      let cap = netCapa.networkCap; 
      if (!cap) { 
        return false; 
      } 
 
      for (let em of cap) { 
        if (connection.NetCap.NET_CAPABILITY_VALIDATED == em) { 
          return true; 
        } 
      } 
    } catch (e) { 
      let err = e as BusinessError 
      console.info('get netInfo error :' + JSON.stringify(err)) 
    } 
    return false; 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
分享
微博
QQ
微信
回复
2024-07-23 18:43:23
相关问题
HarmonyOS 如何判断当前网络是否可用
1014浏览 • 1回复 待解决
HarmonyOS 如何判断当前网络阻塞状态
863浏览 • 1回复 待解决
HarmonyOS 如何快速判断当前网络的类型
1040浏览 • 1回复 待解决
如何判断当前网络是否是Wi-Fi环境?
1362浏览 • 1回复 待解决
判断当前网络环境是否支持IPV4或PV6
1462浏览 • 1回复 待解决
如何判断移动流量热点网络
2561浏览 • 1回复 待解决
网络判断,请问该如何识别?
1211浏览 • 1回复 待解决
如何指定或判断当前编译架构
1098浏览 • 1回复 待解决
求大佬告知如何判断当前版本
2486浏览 • 1回复 待解决
preview能否访问网络
763浏览 • 1回复 待解决
如何对监听判断VPN类型网络
1020浏览 • 1回复 待解决
HarmonyOS 手机如何判断当前横竖屏状态
1387浏览 • 1回复 待解决
如何判断当前设备是不是折叠屏?
1750浏览 • 1回复 待解决
HarmonyOS如何获取当前网络状态
1658浏览 • 1回复 待解决
HarmonyOS 判断网络是否连接
586浏览 • 1回复 待解决
如何判断应用使用流量网络非wifi
1339浏览 • 1回复 待解决
如何判断当前线程是否是主线程
3316浏览 • 1回复 待解决
判断模型能否在手机上运行
2590浏览 • 1回复 待解决
如何判断当前是release包还是debug包
2130浏览 • 1回复 待解决