Android 监听WIFI

[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/archive/2013/01/10/2855096.html]

1.注册一个广播接收器,监听网络状态

1     private void Start()

2     {

3         IntentFilter filter = new IntentFilter();

4         filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

5         filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

6         filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

7         this.registerReceiver(myBroadcastReceiver, filter);

8     }

2.取消注册广播器

1     private void Stop()

2     {

3         this.unregisterReceiver(myBroadcastReceiver);

4     }

3.新建监听类NetworkConnectChangedReceiver继承BroadcastReceiver

 1 public class NetworkConnectChangedReceiver extends BroadcastReceiver

 2 {

 3     @Override

 4     public void onReceive(Context context, Intent intent)

 5     {

 6         // 这个监听wifi的打开与关闭,与wifi的连接无关

 7         if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction()))

 8         {

 9             int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

10             switch (wifiState)

11             {

12             case WifiManager.WIFI_STATE_ENABLED:

13                 Log.e("APActivity", "WIFI_STATE_ENABLED");

14                 break;

15             case WifiManager.WIFI_STATE_ENABLING:

16                 Log.e("APActivity", "WIFI_STATE_ENABLING");

17                 break;

18             case WifiManager.WIFI_STATE_DISABLED:

19                 Log.e("APActivity", "WIFI_STATE_DISABLED");

20                 break;

21             case WifiManager.WIFI_STATE_DISABLING:

22                 Log.e("APActivity", "WIFI_STATE_DISABLING");

23                 break;

24             }

25         }

26         // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager.WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。

27         // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,当然刚打开wifi肯定还没有连接到有效的无线

28         if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction()))

29         {

30             Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

31             if (null != parcelableExtra)

32             {

33                 NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;

34                 switch (networkInfo.getState())

35                 {

36                 case CONNECTED:

37                     Log.e("APActivity", "CONNECTED");

38                     break;

39                 case CONNECTING:

40                     Log.e("APActivity", "CONNECTING");

41                     break;

42                 case DISCONNECTED:

43                     Log.e("APActivity", "DISCONNECTED");

44                     break;

45                 case DISCONNECTING:

46                     Log.e("APActivity", "DISCONNECTING");

47                     break;

48                 case SUSPENDED:

49                     Log.e("APActivity", "SUSPENDED");

50                     break;

51                 case UNKNOWN:

52                     Log.e("APActivity", "UNKNOWN");

53                     break;

54                 default:

55                     break;

56                 }

57             }

58         }

59         // 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。

60         // 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。见log

61         // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,我觉得还是用上边两个配合比较合适

62         if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))

63         {

64             NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

65             if (info != null)

66             {

67                 StringBuilder sb = new StringBuilder();

68                 sb.append("info.getTypeName() : " + info.getTypeName() + "\n");

69                 sb.append("getSubtypeName() : " + info.getSubtypeName() + "\n");

70                 sb.append("getState() : " + info.getState() + "\n");

71                 sb.append("getDetailedState() : " + info.getDetailedState().name() + "\n");

72                 sb.append("getDetailedState() : " + info.getExtraInfo() + "\n");

73                 sb.append("getType() : " + info.getType());

74                 Log.e("APActivity", sb.toString());

75             }

76         }

77     }

78 }

 

 

 

你可能感兴趣的:(android)