赞
踩
Android开发中,许多功能需要网络连接,所以在开发过程中需要进行手机网络的检测。
1.1 实现思路
ConnectivityManager 主要用于查看网络状态和管理网络连接相关的操作
NetworkInfo 对象包含网络连接的所有信息
注意:
由于在 API23 及以上时,getNetworkInfo(int networkType) 方法已被弃用,取而代之的是:
getAllNetworks();
getNetworkInfo(android.net.Network);
getNetworkInfo(Network network);
所以检测网络状态时需要分版本进行检测
1.2 检测代码
//检测当前的网络状态
//API版本23以下时调用此方法进行检测
//因为API23后getNetworkInfo(int networkType)方法被弃用
public void checkState_23(){
//步骤1:通过Context.getSystemService(Context.CONNECTIVITY_SERVICE)获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//步骤2:获取ConnectivityManager对象对应的NetworkInfo对象
//NetworkInfo对象包含网络连接的所有信息
//步骤3:根据需要取出网络连接信息
//获取WIFI连接的信息
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
Boolean isWifiConn = networkInfo.isConnected();
//获取移动数据连接的信息
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
Boolean isMobileConn = networkInfo.isConnected();
tv_WiFistate.setText("Wifi是否连接:" + isWifiConn);
tv_Network_state.setText("移动数据是否连接:" + isMobileConn);
}
// API 23及以上时调用此方法进行网络的检测
// getAllNetworks() 在API 21后开始使用
//步骤非常类似
public void checkState_23orNew(){
//获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取所有网络连接的信息
Network[] networks = connMgr.getAllNetworks();
//用于存放网络连接信息
StringBuilder sb = new StringBuilder();
//通过循环将网络信息逐个取出来
for (int i=0; i < networks.length; i++){
//获取ConnectivityManager对象对应的NetworkInfo对象
NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
}
}
使用 BroadcastReceiver 广播接收器来接收网络状态(采用系统广播)
- BroadcastReceiver 作为 Android 四大组件之一;
- 如果不懂得如何使用 BroadcastReceiver ,请看我写的另外一篇文章: Android 四大组件:BroadcastReceiver
效果图
3.1 自定义广播接收器
public class NetWorkStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("网络状态发生变化");
//检测API是不是小于23,因为到了API23之后getNetworkInfo(int networkType)方法被弃用
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
//获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取ConnectivityManager对象对应的NetworkInfo对象
//获取WIFI连接的信息
NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//获取移动数据连接的信息
NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
Toast.makeText(context, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();
} else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
Toast.makeText(context, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show();
} else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
Toast.makeText(context, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show();
}
//API大于23时使用下面的方式进行网络监听
}else {
System.out.println("API level 大于23");
//获得ConnectivityManager对象
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取所有网络连接的信息
Network[] networks = connMgr.getAllNetworks();
//用于存放网络连接信息
StringBuilder sb = new StringBuilder();
//通过循环将网络信息逐个取出来
for (int i=0; i < networks.length; i++){
//获取ConnectivityManager对象对应的NetworkInfo对象
NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
}
Toast.makeText(context, sb.toString(),Toast.LENGTH_SHORT).show();
}
}
}
3.2 注册广播
public class MainActivity extends AppCompatActivity {
NetWorkStateReceiver netWorkStateReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//在onResume()方法注册
@Override
protected void onResume() {
if (netWorkStateReceiver == null) {
netWorkStateReceiver = new NetWorkStateReceiver();
}
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(netWorkStateReceiver, filter);
System.out.println("注册");
super.onResume();
}
//onPause()方法注销
@Override
protected void onPause() {
unregisterReceiver(netWorkStateReceiver);
System.out.println("注销");
super.onPause();
}
}
<receiver android:name=".NetWorkStateReceiver">
<intent-filter>
//检测网络变化的acton
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
3.3 添加网络权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。