一.   ConnectivityManager

概要

ConnectivityManager是网络连接相关的管理器,它主要用于查询网络状态并在网络发生改变时发出状态变化通知。这个类主要负责的下列四个方面:

1.  监控网络状态(包括WiFi, GPRS, UMTS等)。

2.  当网络连接改变时发送广播Intent。

3.  当一种网络断开时,试图连接到另一种网络进行故障处理。

4.  提供一系列接口让应用程序查询可获得的网络的粗粒度和细粒度状态。

 比较重要的几个类常量

int

TYPE_BLUETOOTH

The Bluetooth data connection. 蓝牙数据连接

int

TYPE_ETHERNET

The Ethernet data connection. 以太网数据连接

int

TYPE_MOBILE

The Mobile data connection. 移动数据链接

int

TYPE_WIFI

The WIFI data connection. wifi链接

String

CONNECTIVITY_ACTION

网络连接发生改变

int

DEFAULT_NETWORK_PREFERENCE

默认网络连接偏好,建议在config.xml中进行配置.并通过调用 getNetworkPreference() 获取应用的当前设置值。

String

EXTRA_EXTRA_INFO

The lookup key for a string that provides optionally supplied extra information about the network state.查询关键字,提供关于网络状态的信息

String

EXTRA_NETWORK_INFO

建议使用getActiveNetworkInfo() or getAllNetworkInfo()获取网络连接信息

String

EXTRA_NETWORK_TYPE

触发 CONNECTIVITY_ACTION广播的网络类型

比较重要的方法

NetworkInfo

getActiveNetworkInfo() 获取当前连接可用的网络

NetworkInfo[]

getAllNetworkInfo() 获取设备支持的所有网络类型的链接状态信息。

NetworkInfo

getNetworkInfo(int networkType)  获取特定网络类型的链接状态信息

int

getNetworkPreference()  获取当前偏好的网络类型。

boolean

isActiveNetworkMetered()

Returns if the currently active data network is metered.

static boolean

isNetworkTypeValid(int networkType)   判断给定的数值是否表示一种网络

boolean

requestRouteToHost(int networkType, int hostAddress)

Ensure that a network route exists to deliver traffic to the specified host via the specified network interface.

void

setNetworkPreference(int preference)

Specifies the preferred network type.

int

startUsingNetworkFeature(int networkType, String feature)

Tells the underlying networking system that the caller wants to begin using the named feature.

int

stopUsingNetworkFeature(int networkType, String feature)

Tells the underlying networking system that the caller is finished using the named feature.

最后奉上一点点干货:

创建一个项目   我的项目名(testnet-note)

com.example.testnet_note下创建一个Btu1Listener.java   完成 onClick事件

/testNet-note/src/com/example/testnet_note/Btu1Listener.java

package com.example.testnet_note;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView; /**
* ConnectivityManager ConnectivityManager主要管理和网络连接相关的操作
* 相关的TelephonyManager则管理和手机、运营商等的相关信息;WifiManager则管理和wifi相关的信息。 想访问网络状态,首先得添加权限
* NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述,
* 通过其getState()方法获取的State对象则代表着连接成功与否等状态。
*
*/
public class Btu1Listener implements OnClickListener { private Context conntxt; // 上下文
private TextView tv1; // 文本控件
private ConnectivityManager cm; // 主要管理和网络连接相关的操作
private NetworkInfo netInfo;
private String netStatus;
private int color1; public Btu1Listener(Context conntxt) {
this.conntxt = conntxt;
} public void onClick(View v) {
try {
Activity c = (Activity) conntxt;
tv1 = (TextView) c.findViewById(R.id.textView1);
cm = (ConnectivityManager) c
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取代表联网状态的NetWorkInfo对象
netInfo = cm.getActiveNetworkInfo(); // getActiveNetworkInfo() if (netInfo == null) { // 提示没有网络
netStatus = c.getResources().getString(R.string.netStatus0); // 提示文字
color1 = c.getResources().getColor(R.color.red); // 背景颜色
} else { // 网络已连接
// netStatus = c.getResources().getString(R.string.netStatus1);
// color1 = c.getResources().getColor(R.color.green);
// 获得当前连接的网络类型。
// TYPE_MOBILE GPRS网络
if (State.CONNECTED == cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()) {
netStatus = c.getResources().getString(R.string.netStatus2);
color1 = c.getResources().getColor(R.color.rosybrown);
}
// TYPE_WIFI WIFI网络
if (State.CONNECTED == cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()) {
netStatus = c.getResources().getString(R.string.netStatus3);
color1 = c.getResources().getColor(R.color.lavender);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
tv1.setText(netStatus);
tv1.setBackgroundColor(color1);
} } }

/testNet-note/res/layout/activity_main.xml 配制

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testnet_note.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="40dp"
android:maxHeight="90dp"
android:text="@string/hello_world"
android:textSize="29sp" /> <Button
android:id="@+id/tstNetwrkBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/btnShow" /> </RelativeLayout>

/testNet-note/res/values/styles.xml 配制

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">testNet-note</string>
<string name="hello_world">网络测试</string>
<string name="btnShow">网络测试</string> <string name="netStatus0">网络不可用</string>
<string name="netStatus1">当前的网络连接可用</string>
<string name="netStatus2">GPRS网络已连接</string>
<string name="netStatus3">WIFI网络已连接</string> <string name="action_settings">Settings</string> </resources>

/testNet-note/res/values/colors.xml 配制

<?xml version="1.0" encoding="utf-8"?>
<resources> <!-- 淡紫色 -->
<color name="orchid">#DA70D6</color>
<!-- 褐玫瑰红 -->
<color name="rosybrown">#BC8F8F</color>
<!-- 红色 -->
<color name="red">#FF0000</color>
<!-- 绿色 -->
<color name="green">#008000</color> </resources>

现在代码写完了   运行有错误   是权限问题

加两个权限就好了

效果图:

WiFi ╭(╯^╰)╮

开了   数据   2G/(ㄒoㄒ)/~~

感觉有bug    WiFi  数据   都关了  它才是   网络不可用

android ConnectivityManager 检查是否有网络的更多相关文章

  1. 5、android ConnectivityManager获取网络状态

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  2. Android Volley和Gson实现网络数据加载

    Android Volley和Gson实现网络数据加载 先看接口 1 升级接口 http://s.meibeike.com/mcloud/ota/cloudService POST请求 参数列表如下 ...

  3. Android训练课程(Android Training) - 使用Volley传输网络数据(Transmitting Network Data Using Volley)

    使用Volley传输网络数据(Transmitting Network Data Using Volley) Volley 是一个 HTTP 库,它使得在Android应用程序中操作网络更容易,是重要 ...

  4. Android 监听 Android中监听系统网络连接打开或者关闭的实现代码

    本篇文章对Android中监听系统网络连接打开或者关闭的实现用实例进行了介绍.需要的朋友参考下 很简单,所以直接看代码 复制代码 代码如下: package xxx; import android.c ...

  5. Android 判断wifi和4G网络是否开启

    public boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) g ...

  6. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  7. Android 内存检查

    Android 内存检查 本文简单介绍了如何使用 DDMS 和 MAT 工具来对 android 进行内存检查,了解 android 内存的具体占用情况. 步骤1. 使用 DDMS 观察内存的使用情况 ...

  8. Android开发学习之路--网络编程之xml、json

    一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...

  9. Android 获取系统时间和网络时间

    有些时候我们的应用中只能使用网络时间,而不能使用系统的时间,这是为了避免用户关闭了使用网络时间的功能后所产生的误差. 直接上代码. 1.清单文件中网络添加权限. <!-- 访问Internet资 ...

随机推荐

  1. HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  2. java操作xml方式比较与详解(DOM、SAX、JDOM、DOM4J)

    java中四种操作(DOM.SAX.JDOM.DOM4J)xml方式的比较与详解     1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准. ...

  3. (1.1.9)UVA 10930 A-Sequence(模拟)

    /* * UVA_10930_1.cpp * * Created on: 2013年10月7日 * Author: Administrator */ #include <iostream> ...

  4. 转: LRU缓存介绍与实现 (Java)

    引子: 我们平时总会有一个电话本记录所有朋友的电话,但是,如果有朋友经常联系,那些朋友的电话号码不用翻电话本我们也能记住,但是,如果长时间没有联系了,要再次联系那位朋友的时候,我们又不得不求助电话本, ...

  5. tomcat : Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:

    错误 严重: Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  6. Facade外观模式 笔记

    Facede模式: 把内部系统复杂隐藏,提供一个方便统一的接口. 微波炉在界面简单操作下就可以烹饪出美味佳肴, 微波炉内部运作原理,各个组件互相交互运作,使用者并不需要关心.  而且关心的话可能没有多 ...

  7. Android Studio中解决Gradle DSL method not found: &#39;android()&#39;

    近期导入as的项目出了这种问题 这个问题困扰了我非常长时间,好吧,搜了半天全都是runProguard的.最后在stackoverflow上搜到解决的方法了: http://stackoverflow ...

  8. win7 vs2010 安装cocos2d-x

    http://blog.csdn.net/leoncoder/article/details/12523727 新安装搭建cocos2d-X的跳过这里,看以下红色開始: cocos2d-x删除vs20 ...

  9. 《火球——UML大战需求分析》(第1章 大话UML)——1.4 如何学好UML?

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  10. HDU3756

    题意:给定三围空间里面某些点,求构造出一个棱锥,将所有点包含,并且棱锥的体积最小. 输入: T(测试数据组数) n(给定点的个数) a,b,c(对应xyz坐标值) . . . 输出: H(构造棱锥的高 ...