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. android中获取SHA1的代码
  2. Android拍照、录像、录音代码范例
  3. Android 代码实现关机重启
  4. android Dialog弹在底部核心代码
  5. android 代码混淆之后 微信分享不起作用
  6. Android 网络多线程断点下载
  7. Android中判断网络功能是否可用
  8. [Android实例] Android网络收音机项目(内含源码)

随机推荐

  1. 如何启动Android SDK 1.5模拟器
  2. 初涉Android之文件保存
  3. 分享偶的android秘籍
  4. Android(安卓)自定义EditText, 增加设置
  5. 真实可行的android 基站定位代码
  6. Android Studio Gradle 添加.so 支持文件
  7. Android项目打包、Eclipse视图和UI控件
  8. Android保存数据几种常用方法解析
  9. Android:ANT打包常见问题简述
  10. Android下玩JNI的新老三种姿势