打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
安卓如何判断是否连接了网络

The getActiveNetworkInfo() method of ConnectivityManager returns a NetworkInfo instance representing the first connected network interface it can find or null if none of the interfaces are connected. Checking if this method returns null should be enough to tell if an internet connection is available or not.


private boolean isNetworkAvailable() {    ConnectivityManager connectivityManager           = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();    return activeNetworkInfo != null && activeNetworkInfo.isConnected();}

You will also need:


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your android manifest.

Edit:

Note that having an active network interface doesn't guarantee that a particular networked service is available. Network issues, server downtime, low signal, captive portals, content filters and the like can all prevent your app from reaching a server. For instance you can't tell for sure if your app can reach Twitter until you receive a valid response from the Twitter service.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【转】android 实现检查网络是否可用
Android网络连接判断与处理
Android 判断网络类型(WIFI、MOBILE、NONE)
android如何判断当前网络类型(联网,2g,3g,wifi等)
android 网络是否可用
Android之getSystemService
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服