什么是检查Internet连接的解决方案 [英] What is the solution to Check internet connectivity

查看:78
本文介绍了什么是检查Internet连接的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getnetworkInfo "已弃用,解决方案是什么?我正在使用 compilesdkversion 24.

getnetworkInfo' is deprecated , what is the solution? I am using compilesdkversion 24.

推荐答案

您可以使用getActiveNetworkInfo()代替getnetworkinfo,因为以前没有考虑其他一些因素,例如网络状态可能会因应用程序而异因此在使用getnetworkinfo时已弃用文档

you can use getActiveNetworkInfo() instead of getnetworkinfo , because there were some other factors which weren't considered before ,like network state can vary app to app while using getnetworkinfo hence deprecated docs

ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if(activeNetwork!=null && activeNetwork.isConnectedOrConnecting()){
  // yeah we are online
}else{
  // oops! no network
}

注意:在访问网络状态之前,也要进行无效性检查以确认其不为空

Note : put a nullity check too to confirm it is not null before accessing the network status

这篇关于什么是检查Internet连接的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆