判断Android手机是否联网
android 中查看当前是否联网
方法如下:
ConnectivityManager cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cwjManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()){
//do something
//能联网
return true;
}else{
//do something
//不能联网
return false;
}
如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过 ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式。

同时要在manifest里面加个权限


文档如下:
boolean android.net.NetworkInfo.isAvailable()

public boolean isAvailable ()
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include

The device is out of the coverage area for any network of this type.
The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.
The device's radio is turned off, e.g., because airplane mode is enabled.

Returns
true if the network is available, false otherwise


======================================================
如果拟开发一个网络应用的程序,首先考虑是否接入网络,在Android手机中判断是否联网可以通过 ConnectivityManager 类的isAvailable()方法判断,

首先获取网络通讯类的实例

ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); ,

使用cwjManager.getActiveNetworkInfo().isAvailable(); 来返回是否有效,如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,

具体的可以通过ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式,

需要注意的是有关调用需要加入 这个权限,大家在真机上Market和Browser程序都使用了这个方法,来判断是否继续,同时在一些网络超时的时候也可以检查下网络连接是否存在,以免浪费手机上的电力资源。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gumanren/archive/2010/09/08/5870042.aspx

============================================================
因为有Google这个互联网巨头作为强大的后盾,所以Android的互联网应用的非常广泛。如果我们在开发过程中能够很好的考虑到联网的细节,那么将会有很好的互联网用户体验。

在做软件开发的时候软件可能需要判断用户的手机是否能联网,下面就是简单的判断方式:

ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cwjManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()){
//do nothing
}
else
{
Toast.makeText(MainActivity.this,\"无互联网连接\",Toast.LENGTH_SHORT).show();
}


同时千万不要忘了在manifest里面加个权限 ,粗心的朋友一定要记住:

更多相关文章

  1. android 2.2+ 完全退出程序的方法
  2. Android之手机屏幕朝向
  3. 【Android(安卓)Demo】Android中取得手机屏幕大小
  4. 判断客户端类型
  5. android从网站获取json接口数据并放置到spinner
  6. 判断android devices是否联网
  7. Android(安卓)开发判断用户是否安装微信和QQ并跳转过去
  8. Android工程 单元测试
  9. cocos creator android studio多渠道打包

随机推荐

  1. Android blueZ HCI(一个):hciconfig实施和经
  2. Android 5.1部分apk的升级或缓存功能无效
  3. 【视频课程】Android底层开发关键技术—A
  4. TextView 图文聊天
  5. Android内核和驱动程序
  6. android Handler详细使用方法实例
  7. Android中DVM和java中的JVM区别
  8. Android官方技术文档翻译——Gradle 插件
  9. Android的Touch事件处理机制
  10. 【Android】仿UC网络判断效果