Android:监听WIFI网络状态

	//注册一个广播接收器,监听网络状态
	private void registerConnectionReceiver()
	{
		IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
		mConnectionReceiver = new ConnectionChangeReceiver();
		this.registerReceiver(mConnectionReceiver, filter);
	}
	private void unregisterConnectionReceiver()
	{
		this.unregisterReceiver(mConnectionReceiver);
	}
	private class ConnectionChangeReceiver extends BroadcastReceiver
	{
		private volatile boolean isParsingXml = false;
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			// 检查WIFI是否连接上AP
			ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI);//3G:TYPE_MOBILE
			if (wifi.isConnected() && !isParsingXml) 
			{
				String xmlUrl = Constants.Url.SERVER_URL_APP_LIST_XML;
				try 
				{
					isParsingXml = true;					
					ParseXmlAndProcess(xmlUrl);
				}
				catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
		}		
	}

你可能感兴趣的:(android,String,网络,filter,service,action)