[一籃飯特稀原創,轉載請註明出自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如果開啟,關閉,以及串連上可用的串連都會接到監聽。見log61 // 這個廣播的最大弊端是比上邊兩個廣播的反應要慢,如果只是要監聽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 }