---
title: "android 检查网络是否连接了"
author: "80x86"
date: "2009-11-30T16:26:22Z"
tags: []
slug: "android"
draft: false
lastmod: "2009-11-30T17:27:29Z"
id: 537434
url: "https://80x86.iteye.com/blog/537434"
---

##### [\[下载\]php java javascript 相关 api 手册的下载](http://www.80x86.cn/article.asp?id=1799)

 代码如下

 

/\**

 * 

 * @param activity

 * @return boolean return true if the application can access the internet

 \*/

public static boolean hasInternet(Activity activity) {

ConnectivityManager manager = (ConnectivityManager) activity

.getSystemService(Context.CONNECTIVITY\_SERVICE);

NetworkInfo info = manager.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;

 

}

 

需要在AndroidManifest.xml中  
增加权限：

&lt;uses-permission android:name="android.permission.ACCESS\_NETWORK\_STATE"&gt;&lt;/uses-permission&gt;