项目用到,标记记录一下方便下一次使用。
先获取系统服务
(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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。