Android 判断网络 文章分类:移动开发
函数用于判断网络是否可用

Java代码
/*
*@return boolean return true if the application can access the internet
*/
private boolean haveInternet(){
NetworkInfo info=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo();
if(info==null || !info.isConnected()){
return false;
}
if(info.isRoaming()){
//here is the roaming option you can change it if you want to disable internet while roaming, just return false
return true;
}
return true;
}


另一种方法

Java代码
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
boitealerte(this.getString(R.string.alert),"getSystemService rend null");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}

更多相关文章

  1. Gradle DSL method not found: 'android()
  2. Android拍照、录像、录音代码范例
  3. android的listView中设置line
  4. Android(安卓)代码实现关机重启
  5. android上tcpdump 抓包
  6. Android--自定义SeekBarPreference控件
  7. Android自动化测试
  8. android Dialog弹在底部核心代码
  9. android webview js交互 (java和js交互)

随机推荐

  1. MySql优化之InnoDB,4GB内存,多查询的my.i
  2. MySQL性能全面优化方法参考,从CPU,文件系
  3. Mysql5.7中使用group concat函数数据被截
  4. MySQL常见内存不足启动失败的完美解决方
  5. linux下mysql的安装步骤
  6. Mac环境mysql5.7.21 utf8编码问题及解决
  7. CentOS 7.0如何启动多个MySQL实例教程(my
  8. MySQL中count(*)、count(1)和count(col)
  9. 关于避免MySQL替换逻辑SQL的坑爹操作详解
  10. Mysql5.7.17 winx64.zip解压缩版安装配置