Android 监听wifi广播的两种方式

1.XML中声明

 <receiver android:name=".NetworkConnectChangedReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
            </intent-filter>
        </receiver>

 

2.代码中注册

 IntentFilter filter = new IntentFilter();
 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
 registerReceiver(new NetworkConnectChangedReceiver(), filter);

 

3.实现

查看源码

打印?

01 package com.example.testss;
02
03 import android.content.BroadcastReceiver;
04 import android.content.Context;
05 import android.content.Intent;
06 import android.net.ConnectivityManager;
07 import android.net.NetworkInfo;
08 import android.net.NetworkInfo.State;
09 import android.net.wifi.WifiManager;
10 import android.os.Parcelable;
11 import android.util.Log;
12
13 public class NetworkConnectChangedReceiver extends BroadcastReceiver {
14     @Override
15     public void onReceive(Context context, Intent intent) {
16         if(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 这个监听wifi的打开与关闭,与wifi的连接无关
17             int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
18             Log.e("H3c""wifiState" + wifiState);
19             switch (wifiState) {
20             case WifiManager.WIFI_STATE_DISABLED:
21                 break;
22             case WifiManager.WIFI_STATE_DISABLING:
23                 break;
24             //
25             }
26         }
27         // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager.WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。
28         // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,当然刚打开wifi肯定还没有连接到有效的无线
29         if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
30             Parcelable parcelableExtra = intent
31                     .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
32             if (null != parcelableExtra) {
33                 NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
34                 State state = networkInfo.getState();
35                 booleanisConnected = state == State.CONNECTED;// 当然,这边可以更精确的确定状态
36                 Log.e("H3c""isConnected" + isConnected);
37                 if (isConnected) {
38                 else {
39
40                 }
41             }
42         }
43         // 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。.
44         // 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。见log
45         // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,我觉得还是用上边两个配合比较合适
46         if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
47 <p>   ConnectivityManager manager = (ConnectivityManager) context
48 &nbsp;&nbsp;&nbsp;&nbsp;.getSystemService(Context.CONNECTIVITY_SERVICE);
49 &nbsp;&nbsp;    NetworkInfo gprs = manager
50 &nbsp;&nbsp;&nbsp;&nbsp;.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
51 &nbsp;&nbsp;    NetworkInfo wifi = manager
52 &nbsp;&nbsp;&nbsp;&nbsp;.getNetworkInfo(ConnectivityManager.TYPE_WIFI);</p><p>&nbsp;&nbsp;  Log.i(TAG, "网络状态改变:"+ wifi.isConnected() + " 3g:" + gprs.isConnected());</p><p>            NetworkInfo info = intent
53                     .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
54             if (info != null) {
55                 Log.e("H3c""info.getTypeName()" + info.getTypeName());
56                 Log.e("H3c""getSubtypeName()" + info.getSubtypeName());
57                 Log.e("H3c""getState()" + info.getState());
58                 Log.e("H3c""getDetailedState()"
59                         + info.getDetailedState().name());
60                 Log.e("H3c""getDetailedState()" + info.getExtraInfo());
61                 Log.e("H3c""getType()" + info.getType());
62
63                 if (NetworkInfo.State.CONNECTED == info.getState()) {
64                 else if (info.getType() == 1) {
65                     if (NetworkInfo.State.DISCONNECTING == info.getState()) {
66
67                     }
68                 }
69             }
70         }
71     }
72 }</p>


你可能感兴趣的:(Android 监听wifi广播的两种方式)