您的位置:首页 > 理论基础 > 计算机网络

Android网络状态之ConnectivityManager

2016-12-17 17:03 267 查看
java.lang.Object
继承者 android.net.ConnectivityManager


他的英文解释:

Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).


我用有道翻译了下:

类,回答关于网络连接状态的查询。当网络连接发生变化也通知应用程序。得到该类的实例通过调用Context.getSystemService(上下文。连接服务)。


几个相关的网络状态管理类

ConnectivityManager:主要管理和网络连接相关的操作

TelephonyManager:则管理和手机、运营商等的相关信息

WifiManager :则管理和wifi相关的信息。 想访问网络状态,


下面咱们直接看代码:

这个dome是一个很简单的,一个MainActivity和两个公共类,其中一个是集成了BroadcastReceiver!如下图所示:



注意,清单文件中的权限一定要加。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


>首先看一下NetUtils这个类(普通无继承),类里写了三个方法:

方法一:netStete(Context context)–检查网络链接状态

方法二:testConnectivityManager(Context context)–检查GPRS链接状态

方法三: testWIFIManager(Context context)–检查wifi链接状态

//创建一个网络管理对象
public static ConnectivityManager conn;


public static boolean netStete(Context context) {
// 获取系统的网络管理对象
conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conn == null) {
return false;
} else {
// 因为网络有wifi和gprs所以用数组来接受
NetworkInfo[] infos = conn.getAllNetworkInfo();
if (infos != null) {
for (NetworkInfo info : infos) {
// 判断网络是否连接了
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}


//GPRS网络状态
public static boolean testConnectivityManager(Context context) {
conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo.State state = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if(NetworkInfo.State.CONNECTED==state){
Log.i("Text", "GPRS网络已连接");
return true;
}

4000
return false;
}


//wifi网络状态
public static boolean testWIFIManager(Context context) {
conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo.State state = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
state = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if(NetworkInfo.State.CONNECTED==state){
Log.i("Text", "WIFI网络已连接");
return true;
}
return false;
}


上面是封装好的方法,直接在MainActivity中引用即可!,例如下面代码

if(!NetUtils.netStete(MainActivity.this)){
//假如没有网络
Ly_false.setVisibility(View.VISIBLE);
Log.i("Text", "img_false11111111111");
}else {
//假如有网络
Ly_ture.setVisibility(View.VISIBLE);
Log.i("Text", "img_false222222222");
}


现在我们再来看一下用BroadcastReceiver怎么运用

这个公共类NetUtisBroad,继承了BroadcastReceiver

这里面有两个方法。

onReceive()

isNetworkAvailable()

public void onReceive(Context context, Intent intent) {
Log.i("Text", "网络状态.");
if (!isNetworkAvailable(context)) {
Toast.makeText(context, "网络断开了!", Toast.LENGTH_SHORT).show();
}
}


public static boolean isNetworkAvailable(Context context) {
//获取网络管理对象
ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取所有可用网络
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}


然后我们需要在AndroidManifest.xml中配置相关信息

<receiver android:name=".NetUtisBroad">
<intent-filter>
<action android:name="com.example.com.CONNECTIVITY_CHANGE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>


我们看一下在MainActivity中的引用

Intent intent = new Intent();
intent.setAction("com.example.com.CONNECTIVITY_CHANGE");
intent.putExtra("df","fdsafadsaafds");
sendBroadcast(intent);


dome下载地址:dome

http://download.csdn.net/detail/bobo8945510/9626948
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: