获取Wifi信息及注册Wifi改变信息

项目用到,标记记录一下方便下一次使用。

先获取系统服务

(ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);

注册广播

IntentFilter ifilter = new IntentFilter(
                    WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
            ifilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            ifilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

            mContext.registerReceiver(mWifiStateReceiver, ifilter);

接收广播

public class WifiStateReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				AspLog.v(TAG,
						"WifiStateReceiver onReceive:" + intent.getAction());
				if (intent.getAction().equals(
						WifiManager.WIFI_STATE_CHANGED_ACTION)) {
					if (dialog_loading != null) {
						AspLog.v(TAG,
								"WIFI_STATE_CHANGED_ACTION dialog_loading="
										+ dialog_loading + ",showing="
										+ dialog_loading.isShowing()
										+ ",wifistate=" + getWIFIstate());
					} else {
						AspLog.v(TAG,
								"WIFI_STATE_CHANGED_ACTION dialog_loading="
										+ dialog_loading + ",showing=null"
										+ ",wifistate=" + getWIFIstate());

					}
					if (dialog_loading != null && dialog_loading.isShowing()
							&& getWIFIstate() == State.CONNECTED) {
						dialog_loading.dismiss();
						finishAsOk(intent);
					}
				}
				if (intent.getAction().equals(
						WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {// 判断wifi
																		// enable的事件
					if (mWifiList == null || mWifiList.size() == 0) {
						mWifiList = mWifiManager.getScanResults();
						listScanResults();
					}
					handlerWIFIMessage(STATE_UPDATE_LIST);
				}
				if (intent.getAction().equals(
						WifiManager.NETWORK_STATE_CHANGED_ACTION)) {// 判断wifi
																	// enable的事件
					mWifiList = mWifiManager.getScanResults();
					listScanResults();
					AspLog.v(TAG, "wifiList size:" + mWifiList.size());
					handlerWIFIMessage(STATE_UPDATE_LIST);
					if (dialog_loading != null) {
						AspLog.v(TAG,
								"NETWORK_STATE_CHANGED_ACTION dialog_loading="
										+ dialog_loading + ",showing="
										+ dialog_loading.isShowing()
										+ ",wifistate=" + getWIFIstate());
					} else {
						AspLog.v(TAG,
								"NETWORK_STATE_CHANGED_ACTION dialog_loading="
										+ dialog_loading + ",showing= null"
										+ ",wifistate=" + getWIFIstate());

					}
					if (dialog_loading != null && dialog_loading.isShowing()
							&& getWIFIstate() == State.CONNECTED) {
						dialog_loading.dismiss();
						finishAsOk(intent);
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

  

更多的移动互联网的发展趋势app开发移动互联网应用相关的资料请到互联网的一点事www.yidin.net 留言

android QQ群:222392467

资料:

http://www.yidin.net/?p=8280

http://www.yidin.net/?p=9725

posted on 2013-05-22 14:47  梁DH  阅读(682)  评论(0编辑  收藏  举报

导航