日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 通过WIFI状态监听广播,判断进入指定wifi范围

發(fā)布時(shí)間:2025/3/11 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 通过WIFI状态监听广播,判断进入指定wifi范围 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:http://blog.csdn.net/kongxiuqi/article/details/52524500

---------------------------------------------

WIFI狀態(tài)變化會(huì)發(fā)送廣播,一些可用的廣播在WifiManger.java中可以看到。 廣播一:WIFI 狀態(tài)開(kāi)關(guān)變化的監(jiān)聽(tīng),enabled,disabled等 /** * Broadcast intent action indicating that Wi-Fi has been enabled, disabled, * enabling, disabling, or unknown. One extra provides this state as an int. * Another extra provides the previous state, if available. * * @see #EXTRA_WIFI_STATE * @see #EXTRA_PREVIOUS_WIFI_STATE */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String WIFI_STATE_CHANGED_ACTION ="android.net.wifi.WIFI_STATE_CHANGED"; // 自定義個(gè)broadcast 在廣播接收器Broadcast中判斷, ?if?(WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction()))?{//這個(gè)監(jiān)聽(tīng)wifi的打開(kāi)與關(guān)閉,與wifi的連接無(wú)關(guān)?? ????????????int?wifiState?=?intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,?0);??? ????????????LogTag.showTAG_e("WIFI狀態(tài)",?"wifiState"+wifiState);?? ????????????switch?(wifiState)?{??? ????????????case?WifiManager.WIFI_STATE_DISABLED:??? ????????????????break;??? ????????????case?WifiManager.WIFI_STATE_DISABLING:??? ????????????????break;??? ???????????//?? ????????????}??? ????????} 各個(gè)狀態(tài)的值如下: /** * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if * it finishes successfully. * * @see #WIFI_STATE_CHANGED_ACTION * @see #getWifiState() */ public static final int WIFI_STATE_DISABLING = 0; /** * Wi-Fi is disabled. * * @see #WIFI_STATE_CHANGED_ACTION * @see #getWifiState() */ public static final int WIFI_STATE_DISABLED = 1; /** * Wi-Fi is currently being enabled. The state will change to {@link #WIFI_STATE_ENABLED} if * it finishes successfully. * * @see #WIFI_STATE_CHANGED_ACTION * @see #getWifiState() */ public static final int WIFI_STATE_ENABLING = 2; /** * Wi-Fi is enabled. * * @see #WIFI_STATE_CHANGED_ACTION * @see #getWifiState() */ public static final int WIFI_STATE_ENABLED = 3; /** * Wi-Fi is in an unknown state. This state will occur when an error happens while enabling * or disabling. * * @see #WIFI_STATE_CHANGED_ACTION * @see #getWifiState() */ public static final int WIFI_STATE_UNKNOWN = 4;廣播二 wifi連接狀態(tài)變化的監(jiān)聽(tīng)/** * Broadcast intent action indicating that the state of Wi-Fi connectivity * has changed. One extra provides the new state * in the form of a {@link android.net.NetworkInfo} object. If the new * state is CONNECTED, additional extras may provide the BSSID and WifiInfo of * the access point. * as a {@code String}. * @see #EXTRA_NETWORK_INFO * @see #EXTRA_BSSID * @see #EXTRA_WIFI_INFO */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String NETWORK_STATE_CHANGED_ACTION = "android.net.wifi.STATE_CHANGE";
?//?這個(gè)監(jiān)聽(tīng)wifi的連接狀態(tài)即是否連上了一個(gè)有效無(wú)線路由,當(dāng)上邊廣播的狀態(tài)是WifiManager.WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的時(shí)候,根本不會(huì)接到這個(gè)廣播。??
????//?在上邊廣播接到廣播是WifiManager.WIFI_STATE_ENABLED狀態(tài)的同時(shí)也會(huì)接到這個(gè)廣播,當(dāng)然剛打開(kāi)wifi肯定還沒(méi)有連接到有效的無(wú)線??
????if?(WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction()))?{??
????????????Parcelable?parcelableExtra?=?intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);????
????????????if?(null?!=?parcelableExtra)?{????
????????????????NetworkInfo?networkInfo?=?(NetworkInfo)?parcelableExtra;????
????????????????State?state?=?networkInfo.getState();??
????????????????boolean?isConnected?=?state==State.CONNECTED;//當(dāng)然,這邊可以更精確的確定狀態(tài)??
????????????????LogTag.showTAG_e(this.getClass().getSimpleName(),?"isConnected"+isConnected);??
????????????????if(isConnected){??
????????????????}else{??
??????????????????????
????????????????}??
????????????}????
????????} ?
廣播三 wifi掃描列表完成 /** * An access point scan has completed, and results are available from the supplicant. * Call {@link #getScanResults()} to obtain the results. {@link #EXTRA_RESULTS_UPDATED} * indicates if the scan was completed successfully. */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SCAN_RESULTS_AVAILABLE_ACTION = "android.net.wifi.SCAN_RESULTS";wifi掃描完成,獲取wili列表 可以通過(guò)wifimanager的startScan主動(dòng)發(fā)起掃描,掃描完成后會(huì)發(fā)送這個(gè)廣播if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {Log.e(TAG, "WifiManager.SCAN_RESULTS_AVAILABLE_ACTION");WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);List<ScanResult> list = wifiManager.getScanResults();for (int i = 0; i < list.size(); i++) {Log.e(TAG, list.get(i).toString());Log.e(TAG, "ScanResult SSID = " + list.get(i).SSID);} }getScanResults 只能得到可用的列表,獲取所有wifi列表需用如下方法,該方法可以獲取已連接過(guò)的但是不在范圍內(nèi)的wifi信息/** * Return a list of all the networks configured in the supplicant. * Not all fields of WifiConfiguration are returned. Only the following * fields are filled in: * <ul> * <li>networkId</li> * <li>SSID</li> * <li>BSSID</li> * <li>priority</li> * <li>allowedProtocols</li> * <li>allowedKeyManagement</li> * <li>allowedAuthAlgorithms</li> * <li>allowedPairwiseCiphers</li> * <li>allowedGroupCiphers</li> * </ul> * @return a list of network configurations in the form of a list * of {@link WifiConfiguration} objects. Upon failure to fetch or * when when Wi-Fi is turned off, it can be null. */ public List<WifiConfiguration> getConfiguredNetworks() {try {return mService.getConfiguredNetworks();} catch (RemoteException e) {Log.w(TAG, "Caught RemoteException trying to get configured networks: " + e);return null;} }廣播四 /** * Broadcast intent action indicating that a connection to the supplicant has * been established (and it is now possible * to perform Wi-Fi operations) or the connection to the supplicant has been * lost. One extra provides the connection state as a boolean, where {@code true} * means CONNECTED. * @see #EXTRA_SUPPLICANT_CONNECTED */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SUPPLICANT_CONNECTION_CHANGE_ACTION ="android.net.wifi.supplicant.CONNECTION_CHANGE";另外還有一個(gè)監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)變化的廣播ConnectivityManager.CONNECTIVITY_ACTION /** * A change in network connectivity has occurred. A default connection has either * been established or lost. The NetworkInfo for the affected network is * sent as an extra; it should be consulted to see what kind of * connectivity event occurred. * <p/> * If this is a connection that was the result of failing over from a * disconnected network, then the FAILOVER_CONNECTION boolean extra is * set to true. * <p/> * For a loss of connectivity, if the connectivity manager is attempting * to connect (or has already connected) to another network, the * NetworkInfo for the new network is also passed as an extra. This lets * any receivers of the broadcast know that they should not necessarily * tell the user that no data traffic will be possible. Instead, the * receiver should expect another broadcast soon, indicating either that * the failover attempt succeeded (and so there is still overall data * connectivity), or that the failover attempt failed, meaning that all * connectivity has been lost. * <p/> * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY * is set to {@code true} if there are no connected networks at all. */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
如果我們需要判斷手機(jī)進(jìn)入到某個(gè)wifi地區(qū)就可以通過(guò)該廣播,以及掃描wifi列表進(jìn)行實(shí)現(xiàn)代碼如下 public class NetworkConnectChangedReceiver extends BroadcastReceiver {private static final String TAG = NetworkConnectChangedReceiver.class.getSimpleName();@Override public void onReceive(Context context, Intent intent) {// 處理掃描完成廣播,根據(jù)掃描結(jié)果比對(duì)是否進(jìn)入了某個(gè)特定wifi范圍if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())) {Log.e(TAG, "WifiManager.SCAN_RESULTS_AVAILABLE_ACTION");WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);List<ScanResult> list = wifiManager.getScanResults(); // 此處循環(huán)遍歷,可以根據(jù)SSID 或者M(jìn)AC,與某個(gè)指定wifi進(jìn)行對(duì)比,如果相等可以認(rèn)為進(jìn)入了該wifi的范圍。 for (int i = 0; i < list.size(); i++) {Log.e(TAG, list.get(i).toString());Log.e(TAG, "ScanResult SSID = " + list.get(i).SSID);}}// 這個(gè)監(jiān)聽(tīng)網(wǎng)絡(luò)連接的設(shè)置,包括wifi和移動(dòng)數(shù)據(jù)的打開(kāi)和關(guān)閉。. // 最好用的還是這個(gè)監(jiān)聽(tīng)。wifi如果打開(kāi),關(guān)閉,以及連接上可用的連接都會(huì)接到監(jiān)聽(tīng)。見(jiàn)log // 這個(gè)廣播的最大弊端是比上邊兩個(gè)廣播的反應(yīng)要慢,如果只是要監(jiān)聽(tīng)wifi,我覺(jué)得還是用上邊兩個(gè)配合比較合適 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);Log.i(TAG, "網(wǎng)絡(luò)狀態(tài)改變:" + wifi.isConnected() + " 3g:" + gprs.isConnected());Log.e(TAG, "ConnectivityManager.CONNECTIVITY_ACTION");NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);if (info != null) {Log.e(TAG, "info.getTypeName()" + info.getTypeName());Log.e(TAG, "getSubtypeName()" + info.getSubtypeName());Log.e(TAG, "getState()" + info.getState());Log.e(TAG, "getDetailedState()" + info.getDetailedState().name());Log.e(TAG, "getDetailedState()" + info.getExtraInfo());Log.e(TAG, "getType()" + info.getType());if (NetworkInfo.State.CONNECTED == info.getState()) {} else if (info.getType() == 1) {if (NetworkInfo.State.DISCONNECTING == info.getState()) {Log.e(TAG, "disconnect");}}} // 開(kāi)啟wifi掃描,掃描完成后發(fā)送廣播 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);wifiManager.startScan(); }} } 需要注冊(cè) mIntentFilter = new IntentFilter();?mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);?mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);?mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);?mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); mIntentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);??registerReceiver(mReceiver, mIntentFilter);
或者在AndroidManifest中添加 <receiver android:name=".receiver.NetworkConnectChangedReceiver"><intent-filter><action android:name="android.net.conn.CONNECTIVITY_CHANGE" /><action android:name="android.net.wifi.WIFI_STATE_CHANGED" /><action android:name="android.net.wifi.STATE_CHANGE" /><action android:name="android.net.wifi.SCAN_RESULTS" /></intent-filter> </receiver>另外需要配置權(quán)限<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />


-------------

更多的Java,Angular,Android,大數(shù)據(jù),J2EE,Python,數(shù)據(jù)庫(kù),Linux,Java架構(gòu)師,:

http://www.cnblogs.com/zengmiaogen/p/7083694.html



總結(jié)

以上是生活随笔為你收集整理的Android 通过WIFI状态监听广播,判断进入指定wifi范围的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。