免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

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

   網上關于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

名稱欄目:Android開發實踐:WIFI掃描功能的封裝
瀏覽路徑:http://m.newbst.com/article38/iidssp.html

成都網站建設公司_創新互聯,為您提供網站排名品牌網站制作用戶體驗微信小程序面包屑導航

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名