91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android開發實踐:WIFI掃描功能的封裝

發布時間:2020-07-29 03:37:43 來源:網絡 閱讀:8354 作者:Jhuster 欄目:移動開發

   網上關于Android WIFI模塊開發的博文已經很多,這里我也想給出我對WIFI模塊編程的一些消化結果,將Android的一些API封裝類,方便在各個Android APP中復用,簡化程序的開發。


   本文先給出一個掃描WIFI功能的封裝成類,創建該類對象后,當你簡單地調用search()函數后,后臺會自動掃描當前的WIFI SSID,并以回調的形式通知你掃描結果,代碼如下,需要注意的地方都用注釋標注出來了。


package com.example.testwifi;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
                                  
public class WifiSearcher {
                                                                                                                                                                                             
    private static final int WIFI_SEARCH_TIMEOUT = 20; //掃描WIFI的超時時間
                                                                                                                                                                                             
    private Context mContext;
    private WifiManager mWifiManager;   
    private WiFiScanReceiver mWifiReceiver;      
    private Lock mLock;
    private Condition mCondition;
    private SearchWifiListener mSearchWifiListener;
    private boolean mIsWifiScanCompleted = false;
                                                                                                                                                                                             
    public static enum ErrorType {
        SEARCH_WIFI_TIMEOUT, //掃描WIFI超時(一直搜不到結果)
        NO_WIFI_FOUND,       //掃描WIFI結束,沒有找到任何WIFI信號
    }
                                                                                                                                                                                             
    //掃描結果通過該接口返回給Caller
    public interface SearchWifiListener {
        public void onSearchWifiFailed(ErrorType errorType);
        public void onSearchWifiSuccess(List<String> results);
    }
                                                                                                                                                                                             
    public WifiSearcher( Context context, SearchWifiListener listener ) {
                                                                                                                                                                                                 
        mContext = context;
        mSearchWifiListener = listener;
                                                                                                                                                                                                 
        mLock = new ReentrantLock();
        mCondition = mLock.newCondition();
        mWifiManager=(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);     
                                                                                                                                                                                                 
        mWifiReceiver = new WiFiScanReceiver();     
    }
                                                                                                                                                                                   
    public void search() {
                                                                                                                                                                                                 
        new Thread(new Runnable() {
                                                                                                                                                                                                     
            @Override
            public void run() {
                //如果WIFI沒有打開,則打開WIFI
                if( !mWifiManager.isWifiEnabled() ) {
                    mWifiManager.setWifiEnabled(true);
                }
                                                                                                                                                                                                                 
                //注冊接收WIFI掃描結果的監聽類對象
                mContext.registerReceiver(mWifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                                                                                                                                                                                                         
                //開始掃描
                mWifiManager.startScan();
                                                                                                                                                                                                                        
                mLock.lock();
                                                                                                                                                                                                         
                //阻塞等待掃描結果
                try {                
                    mIsWifiScanCompleted = false;                
                    mCondition.await(WIFI_SEARCH_TIMEOUT, TimeUnit.SECONDS);
                    if( !mIsWifiScanCompleted ) {
                        mSearchWifiListener.onSearchWifiFailed(ErrorType.SEARCH_WIFI_TIMEOUT);
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                                                                                                                                                                                                         
                mLock.unlock();
                                                                                                                                                                                                         
                //刪除注冊的監聽類對象
                mContext.unregisterReceiver(mWifiReceiver);                          
            }
        }).start();  
    }
                                                                                                                                                                                     
    //系統WIFI掃描結果消息的接收者      
    protected class WiFiScanReceiver extends BroadcastReceiver {
                                                                                                                                                                                                 
        public void onReceive(Context c, Intent intent) {
                                                                                                                                                                                             
            //提取掃描結果
            List<String> ssidResults = new ArrayList<String>();
            List<ScanResult> scanResults = mWifiManager.getScanResults();              
            for(ScanResult result : scanResults ) {
                ssidResults.add(result.SSID);    
            }
                                                                                                                                                                                                     
            //檢測掃描結果
            if( ssidResults.isEmpty() ) {
                mSearchWifiListener.onSearchWifiFailed(ErrorType.NO_WIFI_FOUND);
            }
            else {
                mSearchWifiListener.onSearchWifiSuccess(ssidResults);
            }
                                                                                                                                                                                                     
            mLock.lock();
            mIsWifiScanCompleted = true;
            mCondition.signalAll();  
            mLock.unlock();
        }
    }
}

   代碼中用到了Lock和Condition,就是為了阻塞地等待WIFI掃描的結果,以來可以在search函數內部直接完成監聽對象的registerReceiver和unregisterReceiver,防止由于某些原因忘記unregisterReceiver導致程序crash,另一方面,針對某些Android系統可能不一定能夠及時收到WIFI掃描結果而設置一個超時時間,這樣,不至于UI一直長時間地等待WIFI掃描結果而“假死”。


   對了,AndroidManifest.xml文件中記得添加權限支持哦:


<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>      
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>


   這個WIFI掃描類就分享到這里啦,希望對初學者有幫助,java文件見博文后面的附件,有任何疑問歡迎留言或者來信lujun.hust@gmail.com交流,或者關注我的新浪微博 @盧_俊 獲取最新的文章和資訊。


附件:http://down.51cto.com/data/2364295
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

威信县| 中山市| 西宁市| 德保县| 定西市| 武义县| 上高县| 保康县| 邻水| 万安县| 资溪县| 抚顺市| 敖汉旗| 巴南区| 原阳县| 玛多县| 田林县| 江城| 安乡县| 乐东| 平阳县| 铜鼓县| 北宁市| 措勤县| 忻城县| 德化县| 溧水县| 武城县| 武川县| 梁河县| 莱州市| 霸州市| 德庆县| 延边| 英山县| 离岛区| 隆回县| 阳曲县| 铁岭县| 五莲县| 麟游县|