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里面加个权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

文档如下:
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

更多相关文章

  1. android中设置一些没有maxHeight属性控件的最高值
  2. android sqlite java.lang.IllegalArgumentException: contains
  3. 如何使用ndk中addr2line工具查询so库中错误信息行数
  4. android之Buffer类及子类学习
  5. Android简单文件浏览器源代码
  6. Android中获取当前正在显示的Activity
  7. Android——使用 Broastcast 实现进程间通讯
  8. Android(安卓)ApiDemos示例解析(15):App->Activity->SetWallpaper
  9. Android快速开发工具集合——YUtils

随机推荐

  1. Android实现DES对字符串加密
  2. Android通过uri获取资源路径问题
  3. Android SDK 历史版本
  4. Android能发音的生词本
  5. android:编写属性动画程序(旋转,缩放,淡出淡
  6. android绘制实心。空心五角星
  7. android关于RandomAccessFile
  8. Android Jetpack 之 LiveData
  9. Android(安卓)framework层实现实现wifi无
  10. android itemtouchhelper-extension 仿微