---
title: "Android判断是否有网络连接"
author: "xdwangiflytek"
date: "2013-04-25T16:34:27Z"
tags: ["Android"]
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2013-04-25T16:34:27Z"
id: 1698361
url: "https://xdwangiflytek.iteye.com/blog/1698361"
---

Android中判断有时候因为功能的需求，需要判断是否有网络连接（3g or wifi），代码如下：

 

```
	/**
	 * @descrption 检查是否有网络可用
	 * @author xdwang
	 * @create 2012-9-18下午10:13:42
	 * @return 有网络则返回ture，否则返回false
	 */
	public boolean checkNetwork() {
		boolean flag = false;
		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cwjManager.getActiveNetworkInfo() != null)
			flag = cwjManager.getActiveNetworkInfo().isAvailable();
		return flag;
	}
```

 

需要在AndroidManifest.xml 中加上检查网络的权限  
 &lt;uses-permission android:name="android.permission.ACCESS\_NETWORK\_STATE"/&gt;