在开发中,我们在进行网络读取时,常常需要先判断手机是否联网,如何判断联网呢,请参阅下面关键代码:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetHelper {
//是否联网网络
public static boolean IsHaveInternet(final Context context) {
try {
ConnectivityManager manger = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = manger.getActiveNetworkInfo();
return (info!=null && info.isConnected());
} catch (Exception e) {
return false;
}
}
}

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
  3. Android与js的交互方式
  4. android生成的apk文件反编译源代码然后修改
  5. Android(安卓)Intent 用法全面总结
  6. Android网络状态实时监听器
  7. Android(安卓)NetworkManager
  8. Android新架构组件之Navigation
  9. Android中调用System.exit(0)

随机推荐

  1. Android EventBus使用,粘性事件postStick
  2. Android性能优化(5):APK瘦身优化
  3. Android崩溃后重启
  4. 1、android源代码下载与跟踪
  5. Android如何使用百度地图
  6. Android下得到Home键按下的消息
  7. Android AutoCompleteTextView控件实现类
  8. Android Gradle Study
  9. 绘图机制
  10. 【Android Demo】图片之滑动效果(Gallery