---
title: "android检测网络连接"
author: "w1985chun"
date: "2012-05-07T14:57:33Z"
tags: []
categories: ["移动开发"]
slug: "android"
draft: false
lastmod: "2012-05-07T14:57:33Z"
id: 1513907
url: "https://w1985chun.iteye.com/blog/1513907"
---

```
    ConnectivityManager connMgr = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        // fetch data
    } else {
        // display error
    }
```