应用场景:实现判断当前网络是否可用
当前有可用网络,如下图:
当前没有可用网络,如下图:
实现步骤:
1、获取ConnectivityManager对象
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
2、获取NetworkInfo对象
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
3、判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
4、在AndroidManifest.xml中添加访问当前网络状态权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
代码如下:
public class ClassTestDemoActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (isNetworkAvailable(ClassTestDemoActivity.this))
{
Toast.makeText(getApplicationContext(), "当前有可用网络!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "当前没有可用网络!", Toast.LENGTH_LONG).show();
}
}
/**
* 检查当前网络是否可用
*
* @param context
* @return
*/
public boolean isNetworkAvailable(Activity activity)
{
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null)
{
return false;
}
else
{
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0)
{
for (int i = 0; i < networkInfo.length; i++)
{
System.out.println(i + "===状态===" + networkInfo[i].getState());
System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
// 判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}
今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用。
step1. 编写BroadcastReceiver
[plain] view plaincopy
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.NetworkInfo.State;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.text.format.Formatter;
- import android.util.Log;
-
- import com.canplay.systemui.QuickSettings;
- /**
- * @author ouyangpeng
- * @link http://blog.csdn.net/ouyang_peng
- */
- public class NetworkReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- ConnectivityManager connectivityManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
-
- NetworkInfo net = connectivityManager.getActiveNetworkInfo();
- if (net == null) {
- Log.i("@@@@@@@@@@@@NetworkReceiver", "无网络连接");
- } else {
- Log.i("@@@@@@@@@@@@NetworkReceiver", "网络连接类型为" + net.getTypeName());
- }
- // wifi连接状态
- State wifi = connectivityManager.getNetworkInfo(
- ConnectivityManager.TYPE_WIFI).getState();
- if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
- Log.i("@@@@@@@@@@@@NetworkReceiver", "连接wifi");
- // wifi管理
- WifiManager wifiManager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- StringBuffer sb = new StringBuffer();
- sb.append("\n获取BSSID属性(所连接的WIFI设备的MAC地址):" + wifiInfo.getBSSID());
- // sb.append("getDetailedStateOf() 获取客户端的连通性:");
- sb.append("\n\n获取SSID 是否被隐藏:" + wifiInfo.getHiddenSSID());
- // IP地址转化为字符串格式
- sb.append("\n\n获取IP 地址:" + Formatter.formatIpAddress(wifiInfo.getIpAddress()));
- sb.append("\n\n获取连接的速度:" + wifiInfo.getLinkSpeed());
- sb.append("\n\n获取Mac 地址(手机本身网卡的MAC地址):" + wifiInfo.getMacAddress()==null?"No Wifi Device":wifiInfo.getMacAddress());
- sb.append("\n\n获取802.11n 网络的信号:" + wifiInfo.getRssi());
- sb.append("\n\n获取SSID(所连接的WIFI的网络名称):" + wifiInfo.getSSID());
- sb.append("\n\n获取具体客户端状态的信息:" + wifiInfo.getSupplicantState());
- Log.i("@@@@@@@@@@@@NetworkReceiver", "连接wifi的信息:\n"+sb.toString());
-
- } else if (wifi == State.DISCONNECTED || wifi == State.DISCONNECTING) {
- Log.i("@@@@@@@@@@@@NetworkReceiver", "断开wifi");
- }
-
- }
- }
step2. 配置BroadcastReceiver和相关权限
[java] view plaincopy
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
[java] view plaincopy
- <!-- 检测网络 -->
- <receiver android:name=".NetworkReceiver" >
- <intent-filter>
- <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
- </intent-filter>
- <!-- 开机启动的广播 -->
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- </receiver>
下面是通过logcat打印出来的信息
当wifi连接上时候的信息:
[plain] view plaincopy
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 网络连接类型为WIFI
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 连接wifi
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 连接wifi的信息:
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取BSSID属性(所连接的WIFI设备的MAC地址):20:dc:e6:71:10:e6
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取SSID 是否被隐藏:false
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取IP 地址:192.168.1.115
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取连接的速度:48cc:b2:55:58:f2:02
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取802.11n 网络的信号:-49
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取SSID(所连接的WIFI的网络名称):FORMIKE
- I/@@@@@@@@@@@@NetworkReceiver( 1653):
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 获取具体客户端状态的信息:COMPLETED
当wifi断开时候的信息:
[plain] view plaincopy
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 无网络连接
- I/@@@@@@@@@@@@NetworkReceiver( 1653): 断开wifi